Thursday, April 18, 2013

Accessing DropDownList inside GridView Control

HTML PAGE

<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSource="<%# PopulateDropDownList() %>"
DataTextField="Phone" DataValueField = "PhoneID">
</asp:DropDownList>
</ItemTemplate>




CODE BEHIND

 
 // populates the dropdownlist
public DataSet PopulateDropDownList()
{
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString);
SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM tblPhone", myConnection);
DataSet ds = new DataSet();
ad.Fill(ds, "tblPhone");
return ds;
}


protected void Button2_Click(object sender, EventArgs e)
{
StringBuilder str = new StringBuilder();
foreach (GridViewRow gvr in GridView1.Rows)
{
string selectedText = ((DropDownList)gvr.FindControl("DropDownList1")).SelectedItem.Text;
str.Append(selectedText);
}
Response.Write(str.ToString());
}

No comments:

Post a Comment