【发布时间】:2012-03-07 21:33:20
【问题描述】:
我完全对 ASP.NET GridView 绑定的工作方式感到困惑。
我有一个GridView。现在,在页面加载时(使用!IsPostBack)我正在绑定GridView。
我的 gridview 有一个编辑按钮。当我单击它时,GridView 变为空白。可能会出现这种行为,因为当我单击编辑按钮时,会发生回发,并且因为我在!IsPostback 条件内绑定了GridView,所以它不会绑定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();
}
}
下面是我的页面加载:
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.
}
【问题讨论】:
-
请将您的帖子分成几段,这样它们就不会那么难以阅读。此外,如果可能,请尝试使用正确的大小写和标点符号。
-
向我们展示您的 GridView aspx 标记。 blank 是什么意思,是否启用了 ViewState?
-
您还没有显示整个 GridView,我缺少带有 CommandName="editing" 的按钮。从哪里调用
BindExternalLinks? -
我更新了整个
GridView和我已经粘贴的BindExternalLinks函数,查看上面BindAllExternalLinks函数,希望对你有所帮助 -
你应该使用调试器看看会发生什么。很抱歉没有提供更多帮助。