[英]How can I access asp.net button's “Text” in a code behind method?
In reference to the question of mine at deleting record when it shouldn't 关于我在删除记录时不应该提及的问题
How can I access asp.net button's "Text" in a code behind method? 如何在代码隐藏方法中访问asp.net按钮的“文本”?
Here's how button looks like: 这是按钮的样子:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:Button ID="HiddenButton" Text="" runat="server" OnClick="Deleting_Click" />
This is my code behind: 这是我的代码背后:
protected void Deleting_Click(object sender, EventArgs e)
{
I have already tried: 我已经尝试过了:
HiddenButton.Text = Request.Form["HiddenButton"].ToString();
and 和
Request["__EVENTARGUMENT"];
But nothing has worked, can someone show me the right direction please? 但没有任何效果,有人能告诉我正确的方向吗?
Edit 编辑
Can I use this any way? 我可以用任何方式吗? but not sure: 但不确定:
$(document).ready(function () {
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);
});
Update 更新
What actually happening is, when user clicks on delete linkbutton in GridView this, happens, 实际发生的是,当用户点击GridView中的删除链接按钮时,会发生这种情况,
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton link = e.Row.Cells[4].Controls[2] as LinkButton;
if(link.Text == "Delete")
link.OnClientClick = "return ConfirmationBox("+ ((DataRowView)e.Row.DataItem)["userID"].ToString() +")";
}
}
Now in JS I am catching the action Displaying a messagebox and always returning false, however I am setting text of hidden button so that I can send it to method in code behind for deleting a record using UserID 现在在JS我正在捕捉动作显示消息框并始终返回false, 但是我正在设置隐藏按钮的文本,以便我可以将其发送到代码后面的方法,以便使用UserID删除记录
function ConfirmationBox(userID)
{
var elem = document.getElementById('<%= HiddenButton.ClientID %>');
elem.value = userID;
$.blockUI({ message: $('#question'), css: { width: '275px' }
});
return false;
}
Now I got the ID I needed and can make user choose yes or no, if user clicks yes then this happens, 现在我得到了我需要的ID,可以让用户选择是或否,如果用户点击是,则会发生这种情况,
$('#yes').click(function() {
$.unblockUI();
// $('<%= HiddenButton.ClientID %>').trigger('click');
__doPostBack('<%=HiddenButton.ClientID %>', "");
});
You can cast the sender
to a button and get the information from that. 您可以将sender
转换为按钮并从中获取信息。
protected void Deleting_Click(object sender, EventArgs e)
{
Button btn = (Button) sender;
var text = btn.Text;
}
Update 更新
When choosing (Cast)object
vs object as object
see Direct casting vs 'as' operator? 选择(Cast)object
与object as object
请参阅直接投射vs'as'运算符?
Update 2 更新2
When you want to pass some arguments through to your code which are set via JavaScript, you can pass them through with the EVENTARGUMENT
如果要将一些参数传递给通过JavaScript设置的代码,可以使用EVENTARGUMENT
传递它们
So when calling __doPostBack('','');
所以当调用__doPostBack('','');
You can set it here. 你可以在这里设置它。 In your case you need to update your code to be; 在您的情况下,您需要更新您的代码;
__doPostBack('<%=HiddenButton.ClientID %>', 'Your Argument Here');
Then within your Deleting_Click
method, you can get the argument; 然后在你的Deleting_Click
方法中,你可以获得参数;
string parameter = Request["__EVENTARGUMENT"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.