I am trying to get read values from GridView and upload into database
我试图从GridView获取读取值并上传到数据库
I am able to read the text value but unable to get the GridView CheckBox value ..
我能够读取文本值但无法获取GridView CheckBox值。
I am getting only false (0)
in database table while running the following code in asp.C#
在asp.C中运行以下代码时,我在数据库表中只得到false(0)#
My Code at Button Event is as follows :-
我在Button事件中的代码如下: -
protected void btnSave_Click(object sender, EventArgs e)
{
btnSave.Visible = false;
btnEdit.Visible = true;
int i = 0;
string defaultvalue = "0";
foreach (GridViewRow row in grdExtApp.Rows)
{
string qry = "INSERT into tb_externalsystemaccess(User_Id, SystemName,Access_status) VALUES (@v1, @v2, @v3)";
SqlConnection con = new SqlConnection(str);
using (SqlCommand cmd = new SqlCommand(qry, con))
{
cmd.Parameters.AddWithValue("@v1", TextBox3.Text);
cmd.Parameters.Add("@v2", SqlDbType.Text).Value = Convert.ToString (row.Cells[0].Text);
CheckBox chkBx = (CheckBox)row.FindControl("AccessExternal");
if (chkBx.Checked)
{
defaultvalue = "1";
}
cmd.Parameters.Add("@v3", SqlDbType.Bit).Value = Convert.ToInt16 (defaultvalue);
con.Open();
cmd.ExecuteNonQuery();
i = i + 1;
}
}
}
I am getting the v1 value from outside TextBox control.
我从TextBox控件外部获取v1值。
v2 value is from GridView cell ..it is a string value and I am able to capture it
v2值来自GridView单元格。它是一个字符串值,我能够捕获它
v3 value is CheckBox value ..it is not able capture ..instead shows only as false even though I check the CheckBox.
v3值是CheckBox值..它不能捕获..即使我检查CheckBox,也只显示为false。
My markup for GridView looks like this:
我对GridView的标记如下所示:
<asp:GridView ID="grdExtApp" runat="server"
AutoGenerateColumns="False" BackColor="White">
<Columns>
<asp:BoundField DataField="External_System" HeaderText="External System" />
<asp:TemplateField HeaderText="Access">
<ItemTemplate>
<asp:CheckBox ID="AccessExternal" runat="server" Enabled="true" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Please help me why I am unable to get the checkbox value
请帮助我为什么我无法获得复选框值
2 个解决方案
#1
2
btn_Save()
is a postback method, I am postulating in your Form_Load()
you are always rebinding your grid, thereby clearing out your checked checkboxes.
btn_Save()是一个回发方法,我在你的Form_Load()中假设你总是重新绑定你的网格,从而清除你选中的复选框。
You should have this at the top of your Form_Load()
:
你应该在Form_Load()的顶部有这个:
if (!Page.IsPostBack)
{
grdExtApp.DataSourceID = "yourDatasourceID";
grdExtApp.DataBind();
}
#2
0
Protected void btnSave_Click(object sender, EventArgs e)
{
btnSave.Visible = false;
btnEdit.Visible = true;
//int i = 0;
//string defaultvalue = "0";
string qry = "INSERT into tb_externalsystemaccess(User_Id,SystemName,Access_status) VALUES (@v1, @v2, @v3)";
SqlConnection con = new SqlConnection(str);
for (int i = 0; i < grdExtApp.Rows.Count; i++)
{
GridViewRow row = grdExtApp.Rows[i];
bool defaultvalue = ((CheckBox)row.FindControl("AccessExternal")).Checked;
cmd.Parameters.AddWithValue("@v1", TextBox3.Text);
cmd.Parameters.Add("@v2", SqlDbType.Text).Value = Convert.ToString(row.Cells[0].Text);
cmd.Parameters.Add("@v3", SqlDbType.Bit).Value =defaultvalue;
con.open();
cmd.ExecuteNonQuery();
con.Close();
con.Dispose();
}
}
#1
2
btn_Save()
is a postback method, I am postulating in your Form_Load()
you are always rebinding your grid, thereby clearing out your checked checkboxes.
btn_Save()是一个回发方法,我在你的Form_Load()中假设你总是重新绑定你的网格,从而清除你选中的复选框。
You should have this at the top of your Form_Load()
:
你应该在Form_Load()的顶部有这个:
if (!Page.IsPostBack)
{
grdExtApp.DataSourceID = "yourDatasourceID";
grdExtApp.DataBind();
}
#2
0
Protected void btnSave_Click(object sender, EventArgs e)
{
btnSave.Visible = false;
btnEdit.Visible = true;
//int i = 0;
//string defaultvalue = "0";
string qry = "INSERT into tb_externalsystemaccess(User_Id,SystemName,Access_status) VALUES (@v1, @v2, @v3)";
SqlConnection con = new SqlConnection(str);
for (int i = 0; i < grdExtApp.Rows.Count; i++)
{
GridViewRow row = grdExtApp.Rows[i];
bool defaultvalue = ((CheckBox)row.FindControl("AccessExternal")).Checked;
cmd.Parameters.AddWithValue("@v1", TextBox3.Text);
cmd.Parameters.Add("@v2", SqlDbType.Text).Value = Convert.ToString(row.Cells[0].Text);
cmd.Parameters.Add("@v3", SqlDbType.Bit).Value =defaultvalue;
con.open();
cmd.ExecuteNonQuery();
con.Close();
con.Dispose();
}
}