我在我的web上有一個gridview,並且在模板字段中有複選框。我在數據庫中有一個字段,其中保存整數值0或1. 0爲啓用和1爲禁用。當我檢查我的複選框時,它會在數據庫中爲該特定字段插入1,反之亦然。現在我希望當我打開這個頁面時,表中值爲1的行應該保持檢查狀態,並且表中值爲0的行應該保持不被檢查。我試過這樣做 - 這是我的aspx頁面 -如何保持GridView中的複選框檢查基於一定的條件?
<asp:GridView ID="GridMain" runat="server" Width="1000px"
AutoGenerateColumns="False" onrowdatabound="GridMain_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Student Name">
<ItemTemplate>
<asp:Label ID="lblname" runat="server" Text='<%# Eval("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Enable/Disable">
<ItemTemplate>
<asp:CheckBox ID="chkenbl" runat="server" AutoPostBack="True"
oncheckedchanged="chkenbl_CheckedChanged"
Checked='<%# Convert.ToBoolean(Eval("en_dis")) %>' />
<br />
<asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>'></asp:Label>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("en_dis") %>'
Visible="False"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
這是我的CS PAGE-
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
show();
}
//chkbind();
}
public void show()
{
try
{
dt = g1.return_dt("select id,name,en_dis from tbl_data_show order by name");
if (dt.Rows.Count > 0)
{
adsource = new PagedDataSource();
adsource.DataSource = dt.DefaultView;
adsource.PageSize = 5;
GridMain.DataSource = adsource;
GridMain.DataBind();
}
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
protected void chkenbl_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender;
//CheckBox chk = sender as CheckBox;
//CheckBox chk = (CheckBox)row.FindControl("chkenbl");
GridViewRow row = (GridViewRow)chk.NamingContainer;
if (chk.Checked)
{
try
{
//Label lblid = (Label)GridMain.FindControl("Label1");
//Label lblid = new Label();
//lblid.Text = GridMain.FindControl("Label1").ToString();
string lblid = ((Label)row.FindControl("Label1")).Text;
rows = g1.ExecDB("update tbl_data_show set en_dis='1' where id=" + lblid);
if (rows > 0)
{
Response.Write("Rows Effected Successfull.");
}
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
else
{
//Label lblid1 = (Label)GridMain.FindControl("Label1");
//Label lblid1 = new Label();
//lblid1.Text = GridMain.FindControl("Label1").ToString();
string lblid1 = ((Label)row.FindControl("Label1")).Text;
rows = g1.ExecDB("update tbl_data_show set en_dis='0' where id=" + lblid1);
if (rows > 0)
{
Response.Write("Rows Effected Successfull.");
}
}
}
protected void GridMain_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
dt = g1.return_dt("select en_dis from tbl_data_show");
if (dt.Rows.Count > 0)
{
// CheckBox chk1 = new CheckBox();
CheckBox chk1 = (CheckBox)e.Row.FindControl("chkenbl");
//CheckBox
if (dt.Rows[0]["en_dis"] == "0")
{
chk1.Checked = false;
}
else
{
chk1.Checked = true;
}
}
}
}
請指引我哪裏做錯了嗎?
作爲建議由你,我已經改變了我的代碼更改。請檢查。但我得到這個錯誤 - 指定的轉換無效。 – Omi
@Omi。如果你正在修改'標記',那麼你應該嘗試更新的解決方案。如果null是可能的值而不是'0'或'1',那麼你必須使用'Convert.ToBoolean' – Hassan
我只在我的表的en_dis字段中存儲1或0。 1-如果複選框被選中。 0-如果複選框未被選中 – Omi