Reputation: 353
How to access gridview cells of a particular selected and checked row. Below are the codes
<asp:GridView ID="GridView1" runat="server" AutoGenerateSelectButton="True">
<Columns>
<asp:TemplateField HeaderText="IsApproved">
<ItemTemplate>
<asp:CheckBox ID="chkApproved" runat="server" CommandName="Approve" />
</ItemTemplate></asp:TemplateField></Columns></asp:GridView>
<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Text="Approve" Value="1"></asp:ListItem>
<asp:ListItem Text="Reject" Value="2"></asp:ListItem>
</asp:RadioButtonList>
The datasource of gridview is set by strongly typed data sets on Page_Load. Now on btnSubmit I want to insert the selected row into my database table if it is checked
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (RadioButtonList1.SelectedValue == "1")
{
foreach (GridViewRow row in GridView1.Rows)
{
var chk = row.FindControl("chkApproved") as CheckBox;
if (chk.Checked)
{
DataSet1TableAdapters.tbl_ApproveTableAdapter ta = new DataSet1TableAdapters.tbl_ApproveTableAdapter();
DataSet1.tbl_ApproveDataTable dt = new DataSet1.tbl_ApproveDataTable();
ta.Insert()// here I've to specify the cells of GV
}
}
}
}
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" />
I've two issues here
i) To access the gridview cells if the row is checked in the checkbox
ii) On my GV select it is automatically set to AutoPostback which I want to disable as it is clearing the selected checkboxes. .
Upvotes: 0
Views: 277
Reputation: 546
Try to use CommandArgument='<%# Container.DataItemIndex %>' attribute in the .
It return index of selected GridView row.
Then on click event you can write logic to fetch value of specific rows and columns of selected gridview row.
For example if you want to access 3rd column of 1st row you can write
GridView.Rows[0].Cells[2].Text in click event or checkedchange event to fetch value.
Upvotes: 0
Reputation: 2509
You need to bind gridview like this
if (!Page.IsPostBack)
{
GridView1.DataSource = source;
GridView1.DataBind();
}
and try. Sure it will give your expected result.
Upvotes: 0
Reputation: 3574
Update panel automatically post back but you can disable autopostback by using AsyncPostBackTrigger. Set the ChildrenAsTriggers property to true and the UpdateMode property to Conditional
<asp:UpdatePanel ID="myPanel" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
<ContentTemplate>
// put your code here to avoid autopost back
</ContentTemplate>
</asp:UpdatePanel>
Another way to disable autopostback is to set AutoPostBack to false...
<asp:RadioButtonList ID="RadioButtonList1" AutoPostBack="False">
Upvotes: 1