_dopostback说明

<span style="font-family: Verdana;">
<p>有的时候想在客户端触发服务器端控件的click事件 <br>我们这么写__doPostBack('id','') <br>但是为什么有的时候会实效? <br>因为__doPostBack()函数并没有在页面产生 <br>(如果没有类似linkbutton等控件的话asp.net引擎不产生这段js) <br>解决方法如下: <br>比如前台页面 </p>
<div style="">
<div>
<img width="11" height="16" align="top" src="/Images/OutliningIndicators/None.gif" alt=""><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">asp:Button</span><span style="color: rgb(255, 0, 0);">id</span><span style="color: rgb(0, 0, 255);">="Button1"</span><span style="color: rgb(255, 0, 0);">runat</span><span style="color: rgb(0, 0, 255);">="server"</span><span style="color: rgb(255, 0, 0);">Text</span><span style="color: rgb(0, 0, 255);">="Button"</span><span style="color: rgb(0, 0, 255);">></</span><span style="color: rgb(128, 0, 0);">asp:Button</span><span style="color: rgb(0, 0, 255);">></span> </div>
</div>
<p>(1)</p>
<div style="">
<div>
<img width="11" height="16" align="top" src="/Images/OutliningIndicators/None.gif" alt=""><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(255, 0, 0);">href</span><span style="color: rgb(0, 0, 255);">="#"</span><span style="color: rgb(255, 0, 0);">onclick</span><span style="color: rgb(0, 0, 255);">="document.getElementById('Button1').click()"</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">触发服务器端按钮事件</span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(0, 0, 255);">></span> </div>
</div>
<p>(2) <br>利用GetPostBackEventReference给客户端生成__doPostBack() <br>前台 </p>
<div style="">
<div>
<img width="11" height="16" align="top" src="/Images/OutliningIndicators/None.gif" alt=""><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(255, 0, 0);">href</span><span style="color: rgb(0, 0, 255);">="#"</span><span style="color: rgb(255, 0, 0);">onclick</span><span style="color: rgb(0, 0, 255);">="<%=PostBack()%>"</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);">触发服务器端按钮事件</span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(0, 0, 255);">></span> </div>
</div>
<p>后台</p>
<div style="">
<div>
<img width="11" height="16" align="top" src="/Images/OutliningIndicators/None.gif" alt=""><span style="color: rgb(0, 0, 255);">protected</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">PostBack() <br><img width="11" height="16" align="top" src="/Images/OutliningIndicators/ExpandedBlockStart.gif" id="Codehighlighter1_30_102_Open_Image" alt=""><img width="11" height="16" align="top" src="/Images/OutliningIndicators/ContractedBlock.gif" style="display: none;" id="Codehighlighter1_30_102_Closed_Image" alt=""></span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_30_102_Closed_Text"><img width="15" height="20" src="/Images/dot.gif" alt=""></span><span id="Codehighlighter1_30_102_Open_Text"><span style="color: rgb(0, 0, 0);">{ <br><img width="11" height="16" align="top" src="/Images/OutliningIndicators/InBlock.gif" alt=""></span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.Page.GetPostBackEventReference(</span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.Button1,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">haha</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">); <br><img width="11" height="16" align="top" src="/Images/OutliningIndicators/ExpandedBlockEnd.gif" alt="">}</span></span> </div>
</div>
通过__EVENTARGUMENT="haha"可以判断是不是点了那个链接的PostBack <br>把Button1的按钮事件这么写: <br><div style="">
<div>
<img width="11" height="16" align="top" src="/Images/OutliningIndicators/None.gif" alt=""><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(Request[</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">__EVENTARGUMENT</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">haha</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">) <br><img width="11" height="16" align="top" src="/Images/OutliningIndicators/ExpandedBlockStart.gif" id="Codehighlighter1_43_86_Open_Image" alt=""><img width="11" height="16" align="top" src="/Images/OutliningIndicators/ContractedBlock.gif" style="display: none;" id="Codehighlighter1_43_86_Closed_Image" alt=""></span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_43_86_Closed_Text"><img width="15" height="20" src="/Images/dot.gif" alt=""></span><span id="Codehighlighter1_43_86_Open_Text"><span style="color: rgb(0, 0, 0);">{ <br><img width="11" height="16" align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">Response.Write(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">这个是链接的PostBack</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">); <br><img width="11" height="16" align="top" src="/Images/OutliningIndicators/ExpandedBlockEnd.gif" alt="">}</span></span><span style="color: rgb(0, 0, 0);"> <br><img width="11" height="16" align="top" src="/Images/OutliningIndicators/None.gif" alt=""></span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"> <br><img width="11" height="16" align="top" src="/Images/OutliningIndicators/ExpandedBlockStart.gif" id="Codehighlighter1_99_143_Open_Image" alt=""><img width="11" height="16" align="top" src="/Images/OutliningIndicators/ContractedBlock.gif" style="display: none;" id="Codehighlighter1_99_143_Closed_Image" alt=""></span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_99_143_Closed_Text"><img width="15" height="20" src="/Images/dot.gif" alt=""></span><span id="Codehighlighter1_99_143_Open_Text"><span style="color: rgb(0, 0, 0);">{ <br><img width="11" height="16" align="top" src="/Images/OutliningIndicators/InBlock.gif" alt="">Response.Write(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">这个不是链接的PostBack</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">); <br><img width="11" height="16" align="top" src="/Images/OutliningIndicators/ExpandedBlockEnd.gif" alt="">}</span></span> </div>
</div>
</span>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值