Does not appear to be emitting correct GridView markup for __doPostBack

梦想与她 提交于 2019-12-21 19:51:55

问题


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> 

回答1:


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();
}


来源:https://stackoverflow.com/questions/4862305/does-not-appear-to-be-emitting-correct-gridview-markup-for-dopostback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!