How to access the BoundColumn information on SelectedIndexChanged event of a datagrid
<asp:Label id=’Label1′ runat=’server’>Label</asp:Label> <asp:DataGrid id=’DataGrid1′ OnSelectedIndexChanged=SelectedIndexChg AutoGenerateColumns=’False’ runat=’server’> <Columns> <asp:ButtonColumn Text =’Select’ ButtonType =PushButton CommandName =’Select’ ></asp:ButtonColumn> <asp:BoundColumn Headertext=’ProductId’ DataField=’Productid’></asp:BoundColumn> <asp:BoundColumn Headertext=’ProductName’ DataField=’ProductName’></asp:BoundColumn> </Columns> </asp:DataGrid> VB.NET Protected Sub SelectedIndexChg(ByVal sender As Object, ByVal e As System.EventArgs) Label1.Text = DataGrid1.SelectedItem.Cells(1).Text & DataGrid1.SelectedItem.Cells(2).Text End Sub C# protected void SelectedIndexChg(object sender, System.EventArgs e) { Label1.Text = DataGrid1.SelectedItem.Cells[1].Text + DataGrid1.SelectedItem.Cells[2].Text ; }
How to use a HyperlinkColumn for a DataGrid
<asp:DataGrid id=’DataGrid1′ AutoGenerateColumns=’False’ runat=’server’> <Columns> <asp:HyperLinkColumn HeaderText=’ID’ DataNavigateUrlField=’ID’ DataNavigateUrlFormatString=’page2.aspx?id={0}’ DataTextField=’ID’></asp:HyperLinkColumn> </Columns> </asp:DataGrid>
How to select a specific Item in a DropDownList
Method 1: VB.NET DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue(<value>)) ’DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText(<Text>)) C# DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue(<value>)); //DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText(<Text>)); Method 2: VB.NET DropDownList1.Items.FindByText(‘<Text>’).Selected = true ’DropDownList1.Items.FindByValue(‘<value>’).Selected = true C# DropDownList1.Items.FindByText(‘<Text>’).Selected = true ; //DropDownList1.Items.FindByValue(‘<value>’).Selected = true ;
How to add an extra item to the DropDownList filled with data from a database?
VB.NET ’DropDownList1.Items.Insert(0, ‘Please Select’) DropDownList1.Items.Insert(0, new ListItem(‘<text>’, ‘<value>’)) C# //DropDownList1.Items.Insert(0, ‘Please Select’); DropDownList1.Items.Insert(0, new ListItem(‘<text>’, ‘<value>’)); This should be done after .DataBind of dropdownlist
How to get the textbox value at the client side ?
<script lang=’javascript’> function CheckFunction() { if (document.getElementById(’<%=textbox2.ClientID%>’).value == ”) { alert(‘Please enter a value’); return; } } </script> <asp:textbox id=’textbox2′ runat=’Server’></asp:textbox> <input type=button id=’btn1′ onclick=’javascript:CheckFunction();’ value=’Click’>