C# ASP.net阻止gridview行在编辑模式下单击
我对ASP.NETGridView有一些问题,希望这里的人能帮助我 要求C# ASP.net阻止gridview行在编辑模式下单击,c#,asp.net,C#,Asp.net,我对ASP.NETGridView有一些问题,希望这里的人能帮助我 要求 Gridview整行可单击以打开另一页 在编辑模式下,可以通过edititemtemplate文本框更改表值 问题 文本框选择会触发整行单击事件,并阻止用户更改文本框文本。用户无需打开其他页面即可编辑表格值 ASP页面 代码隐藏 private void BindGrid() { WriteLine(“调用BindGrid”); int proj_code=Int32.Parse(ViewState[“proj_c
- Gridview整行可单击以打开另一页
- 在编辑模式下,可以通过edititemtemplate文本框更改表值
代码隐藏
private void BindGrid()
{
WriteLine(“调用BindGrid”);
int proj_code=Int32.Parse(ViewState[“proj_cd”].ToString());
ProjectRepository remarksRepository=新建ProjectRepository();
var备注=remarks repository.get备注(项目代码、项目任务代码、项目stask1光盘、项目stask2光盘);
remarks gv1.DataSource=备注;
remarks gv1.DataBind();
}
受保护的未命名无效\u单击(对象发送者,事件参数e)
{
Launch.PageEvent(这个,(LinkButton)发送方,ViewState[“proj_cd”].ToString();
}
受保护的void remarks gv1_RowDataBound(对象发送方,GridViewRowEventArgs e)
{
尝试
{
开关(如Row.RowType)
{
案例DataControlRowType.标题:
//...
打破
案例DataControlRowType.DataRow:
e、 添加(“onclick”,Page.ClientScript.GetPostBackEventReference(remarkGV1,“Select$”+e.Row.RowIndex.ToString());
打破
}
}
抓住
{
//…扔
}
如果(e.Row.RowType==DataControlRowType.DataRow)
{
TextBox titleTB=(e.Row.FindControl(“titleTB”)作为TextBox);
int prj_cd=Int32.Parse(ViewState[“proj_cd”].ToString());
ProjectRepository repository=新建ProjectRepository();
var备注=repository.get备注(prj_cd、项目任务代码、项目stask1_cd、项目stask2_cd);
Debug.WriteLine(“备注计数:+备注.Count”);
TMS_PROJ_REMARK REMARK=备注.First(x=>x.REM_ID==Convert.ToInt64(datainder.Eval(e.Row.DataItem,“REM_ID”));
if(titleTB!=null)
{
titleTB.Text=remark.REM_TITLE;
}
}
}
受保护的无效标记GV1\u SelectedIndexChanged(对象发送方,事件参数e)
{
int row=remarks gv1.SelectedIndex;
long rem_id=(long)remarkgv1.DataKeys[row][“rem_id”];
Debug.WriteLine(“REM ID:+REM_ID”);
会话[“编辑备注”]=真;
字典对=新字典();
pairs[“rem_id”]=rem_id.ToString();
pairs[“proj_cd”]=ViewState[“proj_cd”].ToString();
Launch.ToPage(该“项目2.aspx中的备注”,成对);
}
受保护的无效btnAddRemarks\u单击(对象发送者,事件参数e)
{
会话[“新备注”]=正确;
字典对=新字典();
pairs[“proj_cd”]=ViewState[“proj_cd”].ToString();
string url=“项目1.aspx中的项目备注”;
Launch.ToPage(这个,url,pairs);
}
私有void SetTitle()
{
ProjectRepository repository=新建ProjectRepository();
TMS_PROJ_MASTER project=repository.GetProject(Convert.ToInt32(ViewState[“PROJ_cd]”));
this.Title=“备注-”+project.PROJ_Title;
}
受保护的无效标记GV1_行编辑(对象发送方,GridViewEditEventArgs e)
{
remarkGV1.EditIndex=e.NewEditIndex;
BindGrid();
}
受保护的无效标记GV1_行更新(对象发送方,GridViewUpdateEventArgs e)
{
//做事
ProjectRepository repository=新建ProjectRepository();
GridViewRow row=remarkGV1.Rows[e.RowIndex];
TextBox titleTB=(row.FindControl(“titleTB”)作为TextBox);
long rem_id=Convert.ToInt64(remarkgv1.DataKeys[e.RowIndex].Value);
var db=新的thpenties();
TMS_项目备注=db.TMS_项目备注
.第一(x=>x.REM\u ID==REM\u ID);
remark.REM_TITLE=titleTB.Text;
remark.UPD_DATE=DateTime.Now;
db.SaveChanges();
//绑定网格
remarkGV1.EditIndex=-1;
BindGrid();
}
受保护的无效标记GV1_RowCancelingEdit(对象发送方,GridViewCancelEditEventArgs e)
{
remarkGV1.EditIndex=-1;
BindGrid();
}
我建议在代码的这一部分中,编辑行时不要添加整行单击(我已经添加了一个-if-来检查编辑)
<asp:GridView ID="remarksGV1" runat="server" AllowPaging="true" DataKeyNames="REM_ID"
OnRowDataBound="remarksGV1_RowDataBound" OnSelectedIndexChanged="remarksGV1_SelectedIndexChanged"
OnRowEditing="remarksGV1_RowEditing"
OnRowCancelingEdit="remarksGV1_RowCancelingEdit"
OnRowUpdating="remarksGV1_RowUpdating"
AutoPostBack="false"
HeaderStyle-CssClass="thead-dark"
CssClass="table table-striped thead-dark table-borderless table-hover border-0 hand"
AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="No.">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title">
<ItemTemplate>
<asp:Label ID="titleLbl" runat="server" Text='<%# Bind("REM_TITLE") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="titleTB" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="REM_DESC" HeaderText="Remarks Description" ReadOnly="true" />
<asp:BoundField DataField="CREATE_DATE" DataFormatString="{0:d}" HeaderText="Date Created" ReadOnly="true" />
<asp:BoundField DataField="UPD_DATE" DataFormatString="{0:d}" HeaderText="Last Updated" ReadOnly="true" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button Text="Edit" ID="EditBtn" CssClass="btn-action" runat="server" CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button Text="Update" ID="UpdateBtn" CssClass="btn-action" runat="server" CommandName="Update" />
<asp:Button Text="Cancel" ID="CancelBtn" CssClass="btn-action" runat="server" CommandName="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
private void BindGrid()
{
Debug.WriteLine("BindGrid called");
int proj_code = Int32.Parse(ViewState["proj_cd"].ToString());
ProjectRepository remarksRepository = new ProjectRepository();
var remarks = remarksRepository.GetRemarks(proj_code, proj_task_code, proj_stask1_cd, proj_stask2_cd);
remarksGV1.DataSource = remarks;
remarksGV1.DataBind();
}
protected void Unnamed_Click(object sender, EventArgs e)
{
Launch.PageEvent(this, (LinkButton)sender, ViewState["proj_cd"].ToString());
}
protected void remarksGV1_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
switch (e.Row.RowType)
{
case DataControlRowType.Header:
//...
break;
case DataControlRowType.DataRow:
e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(remarksGV1, "Select$" + e.Row.RowIndex.ToString()));
break;
}
}
catch
{
//...throw
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox titleTB = (e.Row.FindControl("titleTB") as TextBox);
int prj_cd = Int32.Parse(ViewState["proj_cd"].ToString());
ProjectRepository repository = new ProjectRepository();
var remarks = repository.GetRemarks(prj_cd, proj_task_code, proj_stask1_cd, proj_stask2_cd);
Debug.WriteLine("Remarks Count: " + remarks.Count);
TMS_PROJ_REMARK remark = remarks.First(x => x.REM_ID == Convert.ToInt64(DataBinder.Eval(e.Row.DataItem, "REM_ID")));
if (titleTB != null)
{
titleTB.Text = remark.REM_TITLE;
}
}
}
protected void remarksGV1_SelectedIndexChanged(object sender, EventArgs e)
{
int row = remarksGV1.SelectedIndex;
long rem_id = (long)remarksGV1.DataKeys[row]["REM_ID"];
Debug.WriteLine("REM ID: " + rem_id);
Session["edit_remarks"] = true;
Dictionary<string, string> pairs = new Dictionary<string, string>();
pairs["rem_id"] = rem_id.ToString();
pairs["proj_cd"] = ViewState["proj_cd"].ToString();
Launch.ToPage(this, "Project_Remarks_In_2.aspx", pairs);
}
protected void btnAddRemarks_Click(object sender, EventArgs e)
{
Session["new_remarks"] = true;
Dictionary<string, string> pairs = new Dictionary<string, string>();
pairs["proj_cd"] = ViewState["proj_cd"].ToString();
string url = "Project_Remarks_In_1.aspx";
Launch.ToPage(this, url, pairs);
}
private void SetTitle()
{
ProjectRepository repository = new ProjectRepository();
TMS_PROJ_MASTER project = repository.GetProject(Convert.ToInt32(ViewState["proj_cd"]));
this.Title = "Remarks - " + project.PROJ_TITLE;
}
protected void remarksGV1_RowEditing(object sender, GridViewEditEventArgs e)
{
remarksGV1.EditIndex = e.NewEditIndex;
BindGrid();
}
protected void remarksGV1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
// Do stuff
ProjectRepository repository = new ProjectRepository();
GridViewRow row = remarksGV1.Rows[e.RowIndex];
TextBox titleTB = (row.FindControl("titleTB") as TextBox);
long rem_id = Convert.ToInt64(remarksGV1.DataKeys[e.RowIndex].Value);
var db = new THPEntities();
TMS_PROJ_REMARK remark = db.TMS_PROJ_REMARK
.First(x => x.REM_ID == rem_id);
remark.REM_TITLE = titleTB.Text;
remark.UPD_DATE = DateTime.Now;
db.SaveChanges();
// Bind Grid
remarksGV1.EditIndex = -1;
BindGrid();
}
protected void remarksGV1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
remarksGV1.EditIndex = -1;
BindGrid();
}
switch (e.Row.RowType)
{
case DataControlRowType.Header:
//...
break;
case DataControlRowType.DataRow:
if((e.Row.RowState & DataControlRowState.Edit) != DataControlRowState.Edit)
e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(remarksGV1, "Select$" + e.Row.RowIndex.ToString()));
break;
}