您好編碼器我有一個搜索用戶頁面,我有一個texbox,按鈕和一個gridview。現在,當我輸入用戶的名稱,然後單擊搜索按鈕時,我可以看到用戶的詳細信息。如何讓用戶在gridview中選擇複選框時出現錯誤
我的數據庫是這樣
我已經綁定我在頁面加載的GridView,這樣我還可以查看用戶的詳細信息,在GridView控件這樣的:
現在在這個gridview中,你可以看到一個IsEnable頭,它有複選框控件。該啓用有點在創建用戶時始終爲真。
我想要的是,當用戶點擊該複選框,該位變成假我在rowdatabound上試試這個,但它給我的對象引用錯誤,你可以告訴我當用戶選擇該複選框時,我會做什麼使我的位錯誤
請幫助我,這得益於對提前
現在,這是我現在用我的代碼做現在告訴我如何更新我能夠從真有點假的行更新
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();
}
}
什麼線是對象引用錯誤嗎?順便說一句,如果(chk.Checked && chk == null)'沒有任何意義。 –
sry我沒有更新,它只有如果(chk.checked)不是如果(chk.checked && chk == null) – amitesh
爲什麼你要通過網格的原料,但然後檢查e.Row? – user844541