【问题标题】:ASP.NET - __doPostBack is not defined error from DataList ItemCommand eventASP.NET - __doPostBack 未定义来自 DataList ItemCommand 事件的错误
【发布时间】:2014-01-19 18:25:36
【问题描述】:

我正在使用 DataList 进行自定义分页实现,并且按钮单击在本地工作,但是在我的 godaddy 服务器中部署它后,按钮单击显示 __doPostBack is not defined 错误

DataList 代码在 Usercontrol

           <asp:DataList CellPadding="1"                        RepeatDirection="Horizontal" runat="server" ID="dlPager"                       onitemcommand="dlPager_ItemCommand">
             <ItemTemplate>
                   <asp:LinkButton Enabled='<%#Eval("Enabled") %>' 
                       runat="server" ID="lnkPageNo" 
                       Text='<%#Eval("Text") %>' 
                       CommandArgument='<%#Eval("Value") %>' 
                       CommandName="PageNo" 
                       BorderStyle="Solid" 
                       BorderWidth="2px" 
                       Font-Bold="True" 
                       Font-Size="Medium" 
                       ForeColor="White" 
                       BackColor="#0066FF" 
                       BorderColor="#66FF33"   
                       Height="20px" 
                       CausesValidation="False">
                   </asp:LinkButton>
            </ItemTemplate></asp:DataList>

并且页面的View Source显示,

  <table id="ctl00_cphBody_ctl00_dlPager" cellspacing="0" cellpadding="1" CausesValidation="False" border="0" style="border-collapse:collapse;">
<tr>
    <td>
                   <input type="button" name="ctl00$cphBody$ctl00$dlPager$ctl00$lnkPageNo" value="1" id="ctl00_cphBody_ctl00_dlPager_ctl00_lnkPageNo" disabled="disabled" style="color:White;background-color:#0066FF;border-color:#66FF33;border-width:2px;border-style:Solid;font-size:Medium;font-weight:bold;height:20px;" />
            </td><td>
                   <input type="button" name="ctl00$cphBody$ctl00$dlPager$ctl01$lnkPageNo" value="2" onclick="javascript:__doPostBack(&#39;ctl00$cphBody$ctl00$dlPager$ctl01$lnkPageNo&#39;,&#39;&#39;)" id="ctl00_cphBody_ctl00_dlPager_ctl01_lnkPageNo" style="color:White;background-color:#0066FF;border-color:#66FF33;border-width:2px;border-style:Solid;font-size:Medium;font-weight:bold;height:20px;" />
            </td><td>
                   <input type="button" name="ctl00$cphBody$ctl00$dlPager$ctl02$lnkPageNo" value="3" onclick="javascript:__doPostBack(&#39;ctl00$cphBody$ctl00$dlPager$ctl02$lnkPageNo&#39;,&#39;&#39;)" id="ctl00_cphBody_ctl00_dlPager_ctl02_lnkPageNo" style="color:White;background-color:#0066FF;border-color:#66FF33;border-width:2px;border-style:Solid;font-size:Medium;font-weight:bold;height:20px;" />
            </td>
</tr>

后面的用户控制代码

protected void dlPager_ItemCommand(object source, DataListCommandEventArgs e)
{
    if (e.CommandName == "PageNo")
    {
        GetData(Convert.ToInt32(e.CommandArgument));
    }
}

页面加载是

    if (!Page.IsPostBack)
    {         
        GetData(1);  
    }

我尝试了其他类似线程中提到的解决方案,但没有奏效。

我可以看到 doPostback scipt 已经存在于页面的视图源中。我正在使用 VS 2010

【问题讨论】:

  • 那么你定义了 __doPostBack 吗??
  • 我没有在用户控件中明确指定它。我尝试了代码 Page.ClientScript.GetPostBackEventReference(this, string.Empty);在 UserControl 的 PageLoad 事件中,但没有工作。
  • 从usercontrol定义doPostBack的正常方式是什么?

标签: javascript asp.net .net-4.0 datalist


【解决方案1】:

您需要将Page.ClientScript.GetPostBackEventReference(this, string.Empty); 添加到链接按钮的OnClick 事件中。所以你必须在dlPager_ItemDataBound 中进行,在那里你可以从e.Item.FindControl 中找到LinkButton。您的代码可能如下所示:

protected void dlPager_ItemDataBound(object sender, DataListItemEventArgs e)
{
    var lb = e.Item.FindControl("lnkPageNo") as LinkButton;
    if (lb != null)
    {
        lb.Attributes["onclick"] = Page.ClientScript.GetPostBackEventReference(this, string.Empty);
    }
}

【讨论】:

  • 确保你的标记中有这个:OnItemDataBound="dlPager_ItemDataBound" 。上面的代码示例已经过测试并且可以正常工作。
  • 我有 OnItemDataBound="dlPager_ItemDataBound" 标记,但不知道为什么这不起作用
  • 打到方法了吗?尝试在var lb = ...设置断点
  • 是的,它确实命中了方法并且正在发生分配。如果您有任何处于工作状态的示例代码,请分享。
  • 用户控件是否在任何类型的 ajax 控件中,例如 UpdatPanel?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多