how to get checkbox & datalist selected value in grid view?

Posted by Shameer.Ali87 under ASP.NET on 12/12/2013 | Points: 10 | Views : 4349 | Status : [Member] | Replies : 3
hi all experts,

I am working on a simple gridview control, that adds some data to database,

my grid view code goes here,

                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowFooter="True"
GridLines="Horizontal" Width="350px" HorizontalAlign="Justify">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="Check" runat="server" /></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="LblName" runat="server" Text='<%#Eval("Name") %>'></asp:Label></ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Gender">
<ItemTemplate>
<asp:DropDownList ID="DDLGender" runat="server" DataTextField='<%Eval("Gender") %>'>
</asp:DropDownList>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlGenders" runat="server">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:Label ID="lblCity" runat="server" Text='<%#Eval("City") %>'></asp:Label></ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtCity" runat="server"></asp:TextBox></FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

here, i have 2 buttons
1. to insert data into database,
2. to delete the data(by selecting the checkbox & clicking delete btn)

here, how can i retrieve the dropdownlist & check box value in codebehind (.cs),

any help will be thankful..

shameer ali shaik


Responses

Posted by: vishalneeraj-24503 on: 12/12/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Write following code:-

On btn_insert event:-

for each(GridviewRow gvr in GridView1.Rows)
{
checkbox Check = (checkbox)gvr.FindControl("Check");
dropdownlist ddlGenders = (dropdownlist)gvr.FindControl("ddlGenders");

if(Check!=null)
{
if(Check.checked)
string val = Check.Text;
}
if(ddlGenders !=null)
{
string val1 = ddlGenders.selectedvalue;
}
}

Do same for delete button.

Shameer.Ali87, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 12/12/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Hi Shameer,
Did you get solution?

Shameer.Ali87, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nismeh on: 12/12/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Shameer,

For that you need to use concept of find control for grid's particular row.
It can be achieved by following
checkbox cbx = (checkbox)grd[1].FindControl("Checkbox1");
dropdownlist ddl = (dropdownlist)gvr[1].FindControl("ddl1");


IT KNOWLEDGE IS APPLIED KNOWLEDGE
So Just Do It

Shameer.Ali87, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response