ASP.NET - __doPostBack未定义DataList ItemCommand事件的错误

时间:2014-01-19 07:38:26

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

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

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>

页面的查看源显示,

  <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已经存在于页面的viewsource中。我正在使用VS 2010

1 个答案:

答案 0 :(得分:1)

您需要将Page.ClientScript.GetPostBackEventReference(this, string.Empty);添加到链接按钮OnClick事件中。因此,您必须在dlPager_ItemDataBound中执行此操作,您可以在LinkButton找到e.Item.FindControl。年份代码可能如下所示:

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