[英]ASP.NET - __doPostBack is not defined error from DataList ItemCommand event
I am using a DataList for custom paging implementation and the button click is working locally but after deploying it in my godaddy server the button click showing a __doPostBack is not defined error 我正在使用DataList进行自定义分页实现,并且按钮单击在本地有效,但是在将其部署到我的Godaddy服务器中后,显示__doPostBack的按钮单击未定义错误
The DataList code is in the Usercontrol 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>
And the View Source of the page shows, 页面的查看源显示,
<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('ctl00$cphBody$ctl00$dlPager$ctl01$lnkPageNo','')" 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('ctl00$cphBody$ctl00$dlPager$ctl02$lnkPageNo','')" 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>
User control code behind 用户控制码后面
protected void dlPager_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "PageNo")
{
GetData(Convert.ToInt32(e.CommandArgument));
}
}
And the page load is 页面加载是
if (!Page.IsPostBack)
{
GetData(1);
}
I tried the solution mentioned in other similar threads but none worked. 我尝试了其他类似线程中提到的解决方案,但没有一个起作用。
I can see the doPostback scipt is already present in the viewsource of the page. 我可以看到页面的视图源中已经存在doPostback scipt。 I am using VS 2010
我正在使用VS 2010
You need to add Page.ClientScript.GetPostBackEventReference(this, string.Empty);
您需要添加
Page.ClientScript.GetPostBackEventReference(this, string.Empty);
to the link button's OnClick
event. 链接按钮的
OnClick
事件。 So you have to do it in dlPager_ItemDataBound
, where you can find the LinkButton
from e.Item.FindControl
. 因此,您必须在
dlPager_ItemDataBound
执行此dlPager_ItemDataBound
,在这里您可以从e.Item.FindControl
找到LinkButton
。 Your code might look like this: 您的代码可能如下所示:
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.