Monday, December 6, 2010

How to get DataKey in GridView's OnRowCommand event

/// <summary>
/// Handle grid Employee row command
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void grdEmployee_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Details")
{
GridViewRow row = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
int index = row.RowIndex;
int EmployeeID = Convert.ToInt32(grdEmployee.DataKeys[index].Value);
}
}

Here “e.CommandSource” is the Clicked LinkButton and NamingContainer is its Parent Control. So that will be the GridViewRow. Here we can easily find the DataKey. Here in my Example EmployeeID is the DataKey

<asp:GridView ID="grdEmployee" runat="server"
EmptyDataText="No Employee Available."
Width="100%" AllowPaging="True"
PageSize="10" OnRowDeleting="grdEmployee_RowDeleting" OnPageIndexChanging="grdEmployee_PageIndexChanging"
DataKeyNames="EmployeeID"
OnRowCommand="grdEmployee_OnRowCommand">
<Columns>
<asp:BoundField HeaderText="Employee Code" DataField="EmployeeCode">
</asp:BoundField>
<asp:BoundField HeaderText="Employee Name" DataField="EmployeeName">
</asp:BoundField>
<asp:TemplateField HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:LinkButton ID="lbtnDelete" runat="server" CausesValidation="false" CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:LinkButton ID="lnkDetails" runat="server" CausesValidation="false" CommandName="Details" Text="View Details"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Friday, December 3, 2010

Difference between RadioButtonList and RadioButton

Difference between RadioButtonList and RadioButton
1. RadioButtonList

RadioButtonList is a single control with a list of RadioButtons.
This is derived from ListControl class. So this will work similar to other list controls like ListBox, DropDownList and CheckBoxList.
For giving a caption for buttons you can use the Text property. You cannot insert a text in between two buttons.
Using the “SelectedIndexChanged” event you will get the selected buttons value (“RadioButtonList1.SelectedValue”).
It is easy to bind this to a DataSource.
private void Bind()
{
RadioButtonList1.DataSource = dsEmployees;
RadioButtonList1.DataTextField = "EmployeeName";
RadioButtonList1.DataValueField = "EmployeeID";
RadioButtonList1.DataBind();
}
If you are using HTML
<asp:RadioButtonList ID="RadioButtonList1" runat="server"
RepeatDirection="Horizontal"
onselectedindexchanged="RadioButtonList1_SelectedIndexChanged">
<asp:ListItem Text="Male" Value="1" ></asp:ListItem>
<asp:ListItem Text="Female" Value="2" ></asp:ListItem>
</asp:RadioButtonList>

2. RadioButton
“”RadioButton” is a single control, it is derived from “CheckBox” Class. You have to set the GroupName property to identify a group. Also the event handler for the event “CheckedChanged” will help us to do some job. Another one thing is you have to write separate handlers for each radio button.

For e.g.:
<asp:RadioButton ID="RadioButton1" runat="server" GroupName="Gender"
AutoPostBack="true" oncheckedchanged="RadioButton1_CheckedChanged" Text="Male" />
<asp:RadioButton ID="RadioButton2" runat="server" GroupName="Gender"
AutoPostBack="true" oncheckedchanged="RadioButton2_CheckedChanged" Text="Female" />