你好编码器我有一个搜索用户页面,我有一个texbox,按钮和gridview。现在当我输入用户名并单击搜索按钮时,我会得到用户的详细信息。
我的数据库是这样的
我已经在页面加载时绑定了我的gridview,这样我也可以在gridview中查看用户的详细信息,如下所示:
现在在此gridview中,您可以看到具有复选框控件的IsEnable标头。启用是一个在创建用户时始终为true的位。
我想要的是当用户点击该复选框时该位变为false我在rowdatabound上尝试这个但是它给了我对象引用错误你们可以告诉我当用户选择那个复选框时我做了什么让我的位错误
请提前帮助我,谢谢
现在我现在用我的代码完成的工作现在告诉我如何在行更新时将我的启用位从true更新为false
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.GetGridData();
}
}
protected void BindGrid()
{
con.Open();
SqlCommand cmd = new SqlCommand("Select * from CreateUser", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
}
else
{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
GridView1.DataSource = ds;
GridView1.DataBind();
int columncount = GridView1.Rows[0].Cells.Count;
GridView1.Rows[0].Cells.Clear();
GridView1.Rows[0].Cells.Add(new TableCell());
GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
GridView1.Rows[0].Cells[0].Text = "No Records Found";
}
}
private void GetGridData()
{
con.Open();
string query = "Select * from CreateUser";
da = new SqlDataAdapter(query, con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
protected void btnSearchUser_Click(object sender, EventArgs e)
{
this.BindGrid();
}
protected void lnkdelete_Click(object sender, EventArgs e)
{
LinkButton lnkbtn = sender as LinkButton;
//getting particular row linkbutton
GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
//getting userid of particular row
int UserID = Convert.ToInt32(GridView1.DataKeys[gvrow.RowIndex].Value.ToString());
string FirstName = gvrow.Cells[0].Text;
con.Open();
SqlCommand cmd = new SqlCommand("delete from CreateUser where UserID=" + UserID, con);
int result = cmd.ExecuteNonQuery();
con.Close();
if (result == 1)
{
BindGrid();
//Displaying alert message after successfully deletion of user
ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('" + FirstName + " details deleted successfully')", true);
this.GetGridData();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//if (e.Row.RowType == DataControlRowType.DataRow)
//{
// //getting username from particular row
// string FirstName = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "FirstName"));
// //identifying the control in gridview
// LinkButton lnkbtnresult = (LinkButton)e.Row.FindControl("lnkdelete");
// //raising javascript confirmationbox whenver user clicks on link button
// lnkbtnresult.Attributes.Add("onclick", "javascript:return ConfirmationBox('" + FirstName + "')");
//}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GetGridData();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label userid= (Label)row.FindControl("lblUserID");
con.Open();
SqlCommand cmd = new SqlCommand("delete FROM CreateUser where UserID='" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()) + "'", con);
cmd.ExecuteNonQuery();
con.Close();
BindGrid();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGrid();
}
protected void GridView1_RowUpdating1(object sender, GridViewUpdateEventArgs e)
{
string query = string.Empty;
string userid = GridView1.DataKeys[e.RowIndex].Values["UserID"].ToString();
//Label id = GridView1.Rows[e.RowIndex].FindControl("lblUserID") as Label;
TextBox FirstName = GridView1.Rows[e.RowIndex].FindControl("txtFirstName") as TextBox;
TextBox LastName = GridView1.Rows[e.RowIndex].FindControl("txtLastName") as TextBox;
TextBox DomainID = GridView1.Rows[e.RowIndex].FindControl("txtDomainID") as TextBox;
TextBox EmailID = GridView1.Rows[e.RowIndex].FindControl("txtEmailID") as TextBox;
TextBox Password = GridView1.Rows[e.RowIndex].FindControl("txtPassword") as TextBox;
TextBox ConfirmPassword = GridView1.Rows[e.RowIndex].FindControl("txtConfirmPassword") as TextBox;
TextBox RoleType = GridView1.Rows[e.RowIndex].FindControl("txtRoleType") as TextBox;
CheckBox IsEnable = GridView1.Rows[e.RowIndex].FindControl("chkIsEnableEdit") as CheckBox;
//TextBox textadd = (TextBox)row.FindControl("txtadd");
//TextBox textc = (TextBox)row.FindControl("txtc");
GridView1.EditIndex = -1;
con.Open();
//SqlCommand cmd = new SqlCommand("SELECT * FROM detail", conn);
SqlCommand cmd = new SqlCommand("update CreateUser set FirstName='" + FirstName.Text + "',LastName='" + LastName.Text + "',DomainID='" + DomainID.Text + "',EmailID='" + EmailID.Text + "',Password='" + Password.Text + "',ConfirmPassword='" + ConfirmPassword.Text + "',RoleType='" + RoleType.Text + "',Enable='" + IsEnable.Checked + "' where UserID='" + userid + "'", con);
cmd.ExecuteNonQuery();
con.Close();
BindGrid();
//GridView1.DataBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGrid();
}
}
答案 0 :(得分:1)
首先放入一些防御性编码,然后找出对象引用错误发生的位置:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
var chk = (CheckBox)e.Row.FindControl("chkEnable");
if (chk == null || !chk.checked) return;
var userId =GridView1.DataKeys[row.RowIndex].Value as string;
if (string.IsNullOrWhiteSpace(userId)) return;
var query = "update CreateUser set Enable='False' where UserID='" + userid + "'";
cmd = new SqlCommand(query, con);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
con.Close();
}
}
您可以使用CheckChanged事件:
<asp:GridView ID="GridView1" runat="server" onrowcommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkEnable" runat="server" AutoPostBack="true" OnCheckedChanged="ChkEnable_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
然后在CheckChanged事件中更新数据库:
protected void ChkEnable_CheckedChanged(object sender, EventArgs e)
{
int selRowIndex = ((GridViewRow)(((CheckBox)sender).Parent.Parent)).RowIndex;
var userId = gridView.DataKeys[selRowIndex].Value as string;
if (string.IsNullOrWhiteSpace(userId)) return;
var checkBox = gridView.Rows[selRowIndex].FindControl("chkEnable") as CheckBox;
if (checkBox == null) return;
var query = "update CreateUser set Enable= @Enabled where UserID='" + userid + "'";
cmd = new SqlCommand(query, con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("Enabled", checkBox.Checked);
cmd.ExecuteNonQuery();
con.Close();
}
答案 1 :(得分:0)
首先,你为什么要在RowDataBound
事件中这样做? RowDataBound出现在一行时
绑定到gridview但在这里我们必须更新gridview行。
如果必须更新行,可以在RowUpdating
命令上执行。
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Checkbox chkbx = ((Checkbox)(row.Cells[8].Controls[0]));
bool IsChecked =chkbx.Checked ;
//You can write your update command here.
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox MyName= (TextBox)e.Row.FindControl("FirstName");
string name = Myname.text;
//For checkbox
Checkbox chkbx = (Checkbox)e.Row.FindControl("IsEnabled");
if(chkbx.Checked ==true)
{
// do your stuff for checkbox checked.
}
else
{
// do your stuff if checkbox is not selected
}
}
}
答案 2 :(得分:0)
最后,我得到了我的回答,感谢您的回复人员
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.GetGridData();
}
}
protected void BindGrid()
{
con.Open();
SqlCommand cmd = new SqlCommand("Select * from CreateUser", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
}
else
{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
GridView1.DataSource = ds;
GridView1.DataBind();
int columncount = GridView1.Rows[0].Cells.Count;
GridView1.Rows[0].Cells.Clear();
GridView1.Rows[0].Cells.Add(new TableCell());
GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
GridView1.Rows[0].Cells[0].Text = "No Records Found";
}
}
private void GetGridData()
{
con.Open();
string query = "Select * from CreateUser";
da = new SqlDataAdapter(query, con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
protected void btnSearchUser_Click(object sender, EventArgs e)
{
this.BindGrid();
}
protected void lnkdelete_Click(object sender, EventArgs e)
{
LinkButton lnkbtn = sender as LinkButton;
//getting particular row linkbutton
GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
//getting userid of particular row
int UserID = Convert.ToInt32(GridView1.DataKeys[gvrow.RowIndex].Value.ToString());
string FirstName = gvrow.Cells[0].Text;
con.Open();
SqlCommand cmd = new SqlCommand("delete from CreateUser where UserID=" + UserID, con);
int result = cmd.ExecuteNonQuery();
con.Close();
if (result == 1)
{
BindGrid();
//Displaying alert message after successfully deletion of user
ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('" + FirstName + " details deleted successfully')", true);
this.GetGridData();
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GetGridData();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label userid= (Label)row.FindControl("lblUserID");
con.Open();
SqlCommand cmd = new SqlCommand("delete FROM CreateUser where UserID='" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()) + "'", con);
cmd.ExecuteNonQuery();
con.Close();
BindGrid();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGrid();
}
protected void GridView1_RowUpdating1(object sender, GridViewUpdateEventArgs e)
{
string query = string.Empty;
string userid = GridView1.DataKeys[e.RowIndex].Values["UserID"].ToString();
//Label id = GridView1.Rows[e.RowIndex].FindControl("lblUserID") as Label;
TextBox FirstName = GridView1.Rows[e.RowIndex].FindControl("txtFirstName") as TextBox;
TextBox LastName = GridView1.Rows[e.RowIndex].FindControl("txtLastName") as TextBox;
TextBox DomainID = GridView1.Rows[e.RowIndex].FindControl("txtDomainID") as TextBox;
TextBox EmailID = GridView1.Rows[e.RowIndex].FindControl("txtEmailID") as TextBox;
TextBox Password = GridView1.Rows[e.RowIndex].FindControl("txtPassword") as TextBox;
TextBox ConfirmPassword = GridView1.Rows[e.RowIndex].FindControl("txtConfirmPassword") as TextBox;
TextBox RoleType = GridView1.Rows[e.RowIndex].FindControl("txtRoleType") as TextBox;
CheckBox IsEnable = GridView1.Rows[e.RowIndex].FindControl("chkIsEnableEdit") as CheckBox;
//TextBox textadd = (TextBox)row.FindControl("txtadd");
//TextBox textc = (TextBox)row.FindControl("txtc");
GridView1.EditIndex = -1;
con.Open();
//SqlCommand cmd = new SqlCommand("SELECT * FROM detail", conn);
SqlCommand cmd = new SqlCommand("update CreateUser set FirstName='" + FirstName.Text + "',LastName='" + LastName.Text + "',DomainID='" + DomainID.Text + "',EmailID='" + EmailID.Text + "',Password='" + Password.Text + "',ConfirmPassword='" + ConfirmPassword.Text + "',RoleType='" + RoleType.Text + "',Enable='" + IsEnable.Checked + "' where UserID='" + userid + "'", con);
cmd.ExecuteNonQuery();
con.Close();
BindGrid();
//GridView1.DataBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGrid();
}
}