为什么不的LinkButton从code执行后面的点击功能 [英] Why is LinkButton not executing the Click function from code behind
问题描述
我有这两个控件的GridView:
I have a GridView which has these two controls:
<asp:Button UseSubmitBehavior="false" runat="server" ID="btnShow" CssClass="btnSearch" Text="View All" CommandName="ViewAll" CommandArgument='<%#((GridViewRow)Container).RowIndex%>' OnClick="btnShow_Click" />
<asp:LinkButton runat="server" ID="btnShow2" CssClass="btnSearch2" Text="View Allst" CommandName="ViewAll" CommandArgument='<%#((GridViewRow)Container).RowIndex%>' PostBackUrl="JavaScript:void(0);" OnClientClick="return false;" OnClick="btnShow_Click">View Alls</asp:LinkButton>
code-背后:
code-behind:
protected void btnShow_Click(object sender, EventArgs e)
{
System.Web.UI.WebControls.Button btn1 = (System.Web.UI.WebControls.Button)(sender);
string strCA = btn1.CommandArgument;
string strCN = btn1.CommandName;
int index = 0;
if (strCN == "ViewAll")
{
index = Convert.ToInt32(strCA);
DataTable cacheTable = HttpContext.Current.Cache["ResultsTable"] as DataTable;
string column = cacheTable.Rows[index].Field<string>("Guideline");
string test = BookingResults.Rows[index].Cells[7].Text;
string html = HttpUtility.HtmlDecode(column);
ResultsDiv.InnerHtml = html;
}
}
JQuery的:
JQuery:
$(document).ready(function () {
//Click the button event!
$(".btnSearch").click(function (e) {
e.preventDefault();
alert($(this).val() + " Clicked");
//centering with css
centerPopup();
//load popup
loadPopup();
});
$(".btnSearch2").click(function (e) {
e.preventDefault();
alert($(this).val() + " Clicked");
//centering with css
centerPopup();
//load popup
loadPopup();
});
$("#popupContactClose").click(function () {
disablePopup();
});
$("#backgroundPopup").click(function () {
disablePopup();
});
//Press Escape event!
$(document).keypress(function (e) {
if (e.keyCode == 27 && popupStatus == 1) {
disablePopup();
}
});
});
var popupStatus = 0;
//loading popup with jQuery magic!
function loadPopup() {
//loads popup only if it is disabled
if (popupStatus == 0) {
$("#backgroundPopup").css({
"opacity": "0.7"
});
$("#backgroundPopup").fadeIn("slow");
$("#popupContact").fadeIn("slow");
popupStatus = 1;
}
alert(popupStatus);
}
//disabling popup with jQuery magic!
function disablePopup() {
//disables popup only if it is enabled
if (popupStatus == 1) {
$("#backgroundPopup").fadeOut("slow");
$("#popupContact").fadeOut("slow");
popupStatus = 0;
}
alert(popupStatus);
}
//centering popup
function centerPopup() {
//request data for centering
var windowWidth = document.documentElement.clientWidth;
var windowHeight = document.documentElement.clientHeight;
var popupHeight = $("#popupContact").height();
var popupWidth = $("#popupContact").width();
//centering
$("#popupContact").css({
"position": "absolute",
"top": windowHeight / 2 - popupHeight / 2,
"left": windowWidth / 2 - popupWidth / 2
});
//only need force for IE6
$("#backgroundPopup").css({
"height": windowHeight
});
}
HTML显示弹出:
HTML that displays the popup:
<div id="popupContact">
<a id="popupContactClose" title="Close Window">x</a>
<h3>Booking Guidelines</h3>
<asp:Panel ID="Panel1" runat="server" style="vertical-align:top" ScrollBars="Vertical" Height="300px" ForeColor="Black">
<div id="ResultsDiv" runat="server" style="vertical-align:top" > </div>
</asp:Panel>
</div>
<div id="backgroundPopup"></div>
GridView控件生成多个行,每行按键都会有不同的索引号引用会话表被用来填充 ResultsDiv.InnerHtml = HTML;
当我点击 btnShow
按钮将显示警报,并显示了更新的 ResultsDiv.InnerHtml = HTML弹出;
使用code-背后的一瞬间,并做了回发,并重新加载页面。
When I click on btnShow
Button it displays the alert and shows the popup with the updated ResultsDiv.InnerHtml = html;
by using the code-behind for a split second and does a postback and reloads the page.
当我点击它显示警报,并显示弹出式窗口,没有做回传btnShow2的LinkButton。我是有唯一的问题,它不访问code-背后更新 ResultsDiv.InnerHtml = HTML;
因此它始终显示相同的结果,不事情的按钮被点击的是哪一行。
When I click 'btnShow2' LinkButton it displays the alert and shows the popup and does not do a postback. The only issue I am having is, it doesn't access the code-behind to update ResultsDiv.InnerHtml = html;
so it is always displaying the same result no matter what row the button is clicked.
如何修改我的code,使其更新 ResultsDiv.InnerHtml = HTML;
,每一个按钮被点击的任何时间显示弹出行和没有做回发?
How do I modify my code so that it updates the ResultsDiv.InnerHtml = html;
and displays the popup every time the button is clicked on any of the row and does NOT do a postback?
推荐答案
如果要删除这两个的OnClientClick =返回false;
和
一项PostBackUrl =JavaScript的:无效(0);
话,肯定会回传。
你可以观察生成/渲染你的HTML,如果你设定回发事件这两个属性 WebForm_DoPostBackWithOptions
这应该是这样
If You Remove Both
OnClientClick="return false;"
and
PostBackUrl="JavaScript:void(0);"
then definitely it will postback.
You can observe your HTML generated/rendered if you set both attributes with Postback event
WebForm_DoPostBackWithOptions
which should be something like
javascript:__doPostBack('BookingResults$ctl02$btnShow2','')
<a href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("BookingResults$ctl03$btnShow2", "", false, "", "JavaScript:void(0);", false, true))" class="btnSearch2" id="BookingResults_btnShow2_1">View Alls</a>
这篇关于为什么不的LinkButton从code执行后面的点击功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!