简体   繁体   English

如何在代码隐藏方法中访问asp.net按钮的“文本”?

[英]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)objectobject 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.

相关问题 我如何使用反射从文件后面的asp.net代码访问服务器端控件? - How can i access a server side control from asp.net code behind file using reflection? 我怎样才能防止asp.net中的方法调用背后的代码 - how can i prevent code behind method call in asp.net 如何在asp.net中的代码后面一段时间后清除标签文本? - How can I clear label text after some amount of time from code behind in asp.net? 如何在后面的代码中访问DetailsView的ItemTemplate的TextBox? ASP.Net C# - How to access a DetailsView's ItemTemplate's TextBox in code behind? ASP.Net C# 在asp.net中,要启用/禁用图像按钮,如何在asp.net代码后面的文件中访问Gridview-&gt; Itemtemplate-&gt;面板的ImageButton控件 - In asp.net, To enable/disable imagebutton, How can I access ImageButton control of Gridview->Itemtemplate->panel in asp.net code behind file ASP.Net-需要单击代码后面的按钮,这样我才能弹出错误消息 - ASP.Net - need to click a button from the code-behind so I can pop an error message 如何在代码中访问ASP.Net向导完成按钮? - How can I Access ASP.Net Wizard Finish Button in Code? 如何在asp.net中的代码后面启用禁用的按钮 - How to enable a disabled button in code behind in asp.net 如何将 Asp.NET 按钮与代码隐藏链接? - How to link Asp.NET Button with Code-behind? ASP.NET按钮未在后面运行代码 - ASP.NET button not running code behind
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM