似乎没有为 __doPostBack 发出正确的 GridView 标记

发布于 2024-10-15 09:36:42 字数 2753 浏览 6 评论 0原文

我问了这个问题,关于一个奇怪的行为ASP.Net 中的 GridView 控件(我使用的是 C#)。

对于 GridView 中的每一行,都有一个“编辑”和“删除”链接。例如,编辑有这样的 javascript:__doPostBack('gvwServers','Edit$0') - 所以显然服务器会发现有人点击了编辑行 0 gvwServers

很公平。如果我单击“编辑”链接,我会收到回发,并且 GridView 会重新绘制,“编辑”按钮将替换为“更新”和“取消”按钮。标准行为。现在 - “取消”按钮有此链接 javascript:__doPostBack('gvwServers','Cancel$0') - 正是我所期望的取消 0 行gvw服务器。但是“更新”按钮有 javascript:__doPostBack('gvwServers$ctl02$ctl00','')。这似乎没有任何意义。这似乎是我处理更新的例程未被触发的原因。

为什么 ASP 不输出正确的回发参数?

我的代码可以在上面的链接中找到。

<asp:GridView ID="gvwServers" runat="server" class="gvwServers"  
AutoGenerateColumns="false"  OnRowEditing="gvwServers_Edit" 
onrowcancelingedit="gvwServers_Cancelling" onrowdeleting="gvwServers_Deleting" 
onrowupdated="gvwServers_Updated" onrowupdating="gvwServers_Updating"
AutoGenerateEditButton=true AutoGenerateDeleteButton=true>

<columns>
    <asp:CommandField  ShowEditButton="true" />
    <asp:CommandField  ShowDeleteButton="true" /> 
    <asp:BoundField DataField="intServerID" visible="false" />

    <asp:TemplateField HeaderText = "Server Name">
        <ItemTemplate>
            <asp:Label ID="lblServerName" runat="server" Text='<%# Bind("txtName") %>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox ID="txtServerName_Edit" runat="server" Text='<%# Bind("txtName") %>'></asp:TextBox>
        </EditItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField  HeaderText = "Key">
        <ItemTemplate>
            <asp:Label ID="lblAppKey" runat="server" Text='<%# Bind("txtApplicationKey") %>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox ID="txtAppKey_Edit" runat="server" Text='<%# Bind("txtApplicationKey") %>'></asp:TextBox>
        </EditItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField  HeaderText = "Connection String">
        <ItemTemplate>
            <asp:Label ID="lblConnString" runat="server" Text='************'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox runat="server" ID="txtConnString_Edit" Width="300px" Height="100px" Text='<%# Bind("txtConnectionString")%>' TextMode="MultiLine" ></asp:TextBox>
        </EditItemTemplate>
    </asp:TemplateField>

</columns>
</asp:GridView> 

I asked this question regarding a strange behaviour from a GridView control in ASP.Net (I'm using C#).

For each row in my GridView there is an an 'Edit' and 'Delete' link. The edit for example has this javascript:__doPostBack('gvwServers','Edit$0') - So obviously the server is going to figure out someone has clicked to edit row 0 of gvwServers.

Fair enough. If I click the Edit link I get a postback and the GridView is redrawn with the Edit button replaced with an 'Update' and 'Cancel' button. Standard behaviour. NOW - The 'Cancel' button has this link javascript:__doPostBack('gvwServers','Cancel$0') - just what I would expect Cancel row 0 of gvwServers. BUT The Update button has javascript:__doPostBack('gvwServers$ctl02$ctl00',''). This appears to not make any sense. And this appears to be the cause of my routine to handle Update not being fired.

Why is ASP not outputting the correct postback arguments?

My code is available at the link above.

<asp:GridView ID="gvwServers" runat="server" class="gvwServers"  
AutoGenerateColumns="false"  OnRowEditing="gvwServers_Edit" 
onrowcancelingedit="gvwServers_Cancelling" onrowdeleting="gvwServers_Deleting" 
onrowupdated="gvwServers_Updated" onrowupdating="gvwServers_Updating"
AutoGenerateEditButton=true AutoGenerateDeleteButton=true>

<columns>
    <asp:CommandField  ShowEditButton="true" />
    <asp:CommandField  ShowDeleteButton="true" /> 
    <asp:BoundField DataField="intServerID" visible="false" />

    <asp:TemplateField HeaderText = "Server Name">
        <ItemTemplate>
            <asp:Label ID="lblServerName" runat="server" Text='<%# Bind("txtName") %>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox ID="txtServerName_Edit" runat="server" Text='<%# Bind("txtName") %>'></asp:TextBox>
        </EditItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField  HeaderText = "Key">
        <ItemTemplate>
            <asp:Label ID="lblAppKey" runat="server" Text='<%# Bind("txtApplicationKey") %>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox ID="txtAppKey_Edit" runat="server" Text='<%# Bind("txtApplicationKey") %>'></asp:TextBox>
        </EditItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField  HeaderText = "Connection String">
        <ItemTemplate>
            <asp:Label ID="lblConnString" runat="server" Text='************'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox runat="server" ID="txtConnString_Edit" Width="300px" Height="100px" Text='<%# Bind("txtConnectionString")%>' TextMode="MultiLine" ></asp:TextBox>
        </EditItemTemplate>
    </asp:TemplateField>

</columns>
</asp:GridView> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迟到的我 2024-10-22 09:36:42

不确定你在期待什么/没有发生什么。我拿了你的 gridview 代码并在另一个链接后面使用了你的代码。我在每个处理程序中添加了一个 Response.Write,它似乎按预期运行。

public class Item
{
    public int intServerID { get; set; }
    public string txtName { get; set; }
    public string txtApplicationKey { get; set; }
    public string txtConnectionString { get; set; }
}

protected void Page_Load(object sender, EventArgs e)
{
    Item item = new Item();
    item.intServerID = 1;
    item.txtName = "Apple";
    item.txtApplicationKey = "Orange";
    item.txtConnectionString = "Test";

    List<Item> items = new List<Item>();
    items.Add(item);

    gvwServers.DataSource = items;
    gvwServers.DataBind();
}

protected void gvwServers_Edit(object sender, GridViewEditEventArgs e)
{
    Response.Write("Edit");
    gvwServers.EditIndex = e.NewEditIndex;
    gvwServers.DataBind();
}
protected void gvwServers_Updated(object sender, GridViewUpdatedEventArgs e)
{
    Response.Write("Updated");
    gvwServers.DataBind();
}

protected void gvwServers_Updating(object sender, GridViewUpdateEventArgs e)
{
    Response.Write("Updating");
    gvwServers.DataBind();
}
protected void gvwServers_Deleting(object sender, GridViewDeleteEventArgs e)
{
    Response.Write("Delete");
    gvwServers.DataBind();
}
protected void gvwServers_Cancelling(object sender, GridViewCancelEditEventArgs e)
{
    Response.Write("Cancel");

    e.Cancel = true;
    gvwServers.EditIndex = -1;
    gvwServers.DataBind();
}

Not sure what you're expecting/not happening. I took your gridview code and used your code behind in the other link. I added a Response.Write in each handler and it seems to run as expected.

public class Item
{
    public int intServerID { get; set; }
    public string txtName { get; set; }
    public string txtApplicationKey { get; set; }
    public string txtConnectionString { get; set; }
}

protected void Page_Load(object sender, EventArgs e)
{
    Item item = new Item();
    item.intServerID = 1;
    item.txtName = "Apple";
    item.txtApplicationKey = "Orange";
    item.txtConnectionString = "Test";

    List<Item> items = new List<Item>();
    items.Add(item);

    gvwServers.DataSource = items;
    gvwServers.DataBind();
}

protected void gvwServers_Edit(object sender, GridViewEditEventArgs e)
{
    Response.Write("Edit");
    gvwServers.EditIndex = e.NewEditIndex;
    gvwServers.DataBind();
}
protected void gvwServers_Updated(object sender, GridViewUpdatedEventArgs e)
{
    Response.Write("Updated");
    gvwServers.DataBind();
}

protected void gvwServers_Updating(object sender, GridViewUpdateEventArgs e)
{
    Response.Write("Updating");
    gvwServers.DataBind();
}
protected void gvwServers_Deleting(object sender, GridViewDeleteEventArgs e)
{
    Response.Write("Delete");
    gvwServers.DataBind();
}
protected void gvwServers_Cancelling(object sender, GridViewCancelEditEventArgs e)
{
    Response.Write("Cancel");

    e.Cancel = true;
    gvwServers.EditIndex = -1;
    gvwServers.DataBind();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文