Monday, June 29, 2009

Gridiview Child Controls Event Firing Sample

<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" BackColor="White"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3"
DataKeyNames="CompanyID" DataSourceID="SqlDataSource1" onrowdatabound="GridView1_RowDataBound1"
>
<RowStyle ForeColor="#000066" />
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:CheckBox ID="chkchoose" runat="server" AutoPostBack="true" OnCheckedChanged="chkchoose_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>


<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Select" Text="Select"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CompanyID" InsertVisible="False"
SortExpression="CompanyID">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("CompanyID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("CompanyID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TargetID" SortExpression="TargetID">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("TargetID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("TargetID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CompanyName" SortExpression="CompanyName">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("CompanyName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("CompanyName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email" SortExpression="Email">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Email") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("Email") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="URL" SortExpression="URL">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("URL") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("URL") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country" SortExpression="Country">
<EditItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("Country") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged" ></asp:DropDownList>

</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MCCMSConnectionString4 %>"
SelectCommand="SELECT [CompanyID], [TargetID], [CompanyName], [Email], [URL], [Country] FROM [CMStbCompanies]"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:MCCMSConnectionString %>"
SelectCommand="SELECT [Acronym], [AcronymID] FROM [CMStbAcronyms] WHERE ([AcrCat] = @AcrCat)">
<SelectParameters>
<asp:Parameter DefaultValue="Countries" Name="AcrCat" Type="String" />
</SelectParameters>
</asp:SqlDataSource>







In Code Behind Page inorder to fire the events

protected void chkchoose_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender;
TableCell cell = chk.Parent as TableCell;
GridViewRow item = cell.Parent as GridViewRow;

int index = item.RowIndex;
string content = item.Cells[0].Text;

if (chk.Checked)
{
Response.Write(String.Format("Checked Row {0} contains {1}", index, content));
}

}

protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList list = (DropDownList)sender;

TableCell cell = list.Parent as TableCell;
GridViewRow item = cell.Parent as GridViewRow;

int index = item.RowIndex;
string content = item.Cells[0].Text;

Response.Write(String.Format("Row {0} contains {1}", index, content));

}


Also You bind the DropDown in Code Behind instead at the Item Template Level


protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("ddlCountry");

if (ddl != null)
{
ddl.DataTextField = "Acronym";
ddl.DataValueField = "AcronymID";
ddl.DataSource = SqlDataSource2;
ddl.DataBind();
}
}
}

No comments: