我完全与ASP.NET GridView
绑定工作方式混淆。我有一个GridView
。现在,在页面加载(使用!IsPostBack
)我绑定GridView
。在ASP.NET中使用GridView绑定问题
我的gridview有一个编辑按钮。当我点击它时,GridView
变为空白。行为是可以预料的,因为当我点击编辑按钮时,发生回发,并且因为我已将GridView
绑定在!IsPostback
条件中,所以它不会绑定GridView
。
现在,如果我删除GridView
绑定,并将其置于!IsPostback
条件之外,则编辑按钮将起作用。但是,我无法从TextBox
获得编辑的值。在这种情况下,行为也是可以预料到的,因为当点击更新按钮时,GridView
被重新绑定,因为这次的绑定已经在!IsPostback
条件之外完成了。
所以,我想知道编辑按钮正常工作的代码是什么,同时可以检索TextBox
的编辑值。
问题与代码更新时间:
<asp:GridView ID="grdExternalLinkSection1" ShowFooter="true" runat="server" Width="100%" AutoGenerateColumns="false" CellPadding="5" EnableViewState="true">
<EmptyDataTemplate>
External Link Title
<asp:TextBox ID="txtExternalLinkTitleEmptySection1" runat="server"></asp:TextBox>
External Link Url
<asp:TextBox ID="txtExternalLinkUrlEmptySection1" runat="server"></asp:TextBox>
<asp:Button ID="btnExternalLinkEmptySection1" OnCommand="grdExternalLinkSection_Button_Clicks" runat="server" Text="Add" CommandName="headernew,1" style="padding:3px; width:56px;" />
</EmptyDataTemplate>
<Columns>
<asp:TemplateField HeaderText="Title">
<ItemTemplate>
<asp:Label ID="lblExternalLinkTitleSection1" runat="server"><%# Eval("Title") %></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtExternalLinkTitleEditSection1" runat="server" Text='<%# Bind("Title") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtExternalLinkTitleFooterSection1" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Url">
<ItemTemplate>
<asp:Label ID="lblExternalLinkUrlSection1" runat="server"><%# Eval("Url")%></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtExternalLinkUrlEditSection1" runat="server" Text='<%# Bind("Url") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtExternalLinkUrlFooterSection1" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Manage">
<ItemTemplate>
<asp:Button ID="btnExternalLinkEditSection1" OnCommand="grdExternalLinkSection_Button_Clicks" runat="server" CommandName="Editing,1" CommandArgument='<%# Container.DataItemIndex %>' Text="Edit" />
<asp:Button ID="btnExternalLinkDeleteSection1" OnCommand="grdExternalLinkSection_Button_Clicks" runat="server" CommandName="Deleting,1" CommandArgument='<%# Container.DataItemIndex %>' Text="Delete" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="btnExternalLinkUpdateSection1" OnCommand="grdExternalLinkSection_Button_Clicks" runat="server" CommandName="Updating,1" CommandArgument='<%# Container.DataItemIndex %>' Text="Update" />
<asp:Button ID="btnExternalLinkCancelSection1" OnCommand="grdExternalLinkSection_Button_Clicks" runat="server" CommandName="Canceling,1" CommandArgument='<%# Container.DataItemIndex %>' Text="Cancel" />
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="btnExternalLinkAddFooterSection1" OnCommand="grdExternalLinkSection_Button_Clicks" runat="server" CommandName="Footer,1" Text="Add" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
下面
是做结合工作中的作用:
GridView grid;
protected void BindExternalLinks(int SectionID, string ControlName)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlDataAdapter adapter = new SqlDataAdapter("user_Newsletter_GetExternalLinks", connection))
{
adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
adapter.SelectCommand.Parameters.Add("@SectionID", SqlDbType.Int).Value = SectionID;
adapter.SelectCommand.Parameters.Add("@PortalID", SqlDbType.Int).Value = PortalID;
DataTable dt = new DataTable();
adapter.Fill(dt);
grid = (GridView)this.FindControl(ControlName);
grid.DataSource = dt;
}
}
}
protected void BindAllExternalLinks()
{
for (int i = 1; i <= NewsLetterSectionCount; i++)
{
BindExternalLinks(i, "grdExternalLinkSection" + i);
grid.DataBind();
}
}
下面
是我的pageLoad的:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindAllExternalLinks();
}
}
以下是我的命令按钮事件:我保留了所有命令按钮的常用处理程序:
protected void grdExternalLinkSection_Button_Clicks(object sender, CommandEventArgs e)
{
int rowIndex = (e.CommandArgument != "") ? Convert.ToInt32(e.CommandArgument) : -1;
string[] commandNames = e.CommandName.ToString().Split(',');
string command = commandNames[0].ToString().ToLower();
int sectionID = Convert.ToInt32(commandNames[1]);
GridView grid = (GridView)this.FindControl("grdExternalLinkSection" + sectionID);
try
{
if (command == "headernew")
{
TextBox title = grid.Controls[0].Controls[0].FindControl("txtExternalLinkTitleEmptySection" + sectionID) as TextBox;
TextBox url = grid.Controls[0].Controls[0].FindControl("txtExternalLinkUrlEmptySection" + sectionID) as TextBox;
UpdateExternalLinks(ModifyExternalLinks.Insert, sectionID, title.Text, url.Text);
MessageShow("External Link Added Successfully");
}
else if (command == "editing")
{
//grid.EditIndex = rowIndex;
}
else if (command == "canceling")
{
grid.EditIndex = -1;
}
else if (command == "footer")
{
Response.Write("Inside Footer");
}
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
BindExternalLinks(sectionID, "grdExternalLinkSection" + sectionID);
grid.DataBind(); //here i am binding once the records are modified.
}
每次回传请求后,请您的文章分成几段,让他们不那么难读。另外,如果可能,请尝试使用正确的大小写和标点符号。 – jadarnel27 2012-03-07 21:43:42
向我们展示您的GridView aspx标记。什么意思_blank_并启用了ViewState? – 2012-03-07 21:43:47
你还没有显示整个GridView,我错过了CommandName =“editing”的按钮。从哪里调用BindExternalLinks? – 2012-03-07 21:53:47