<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>