0

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?

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,

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

   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,

$('#yes').click(function() { 
            $.unblockUI(); 

          //  $('<%= HiddenButton.ClientID %>').trigger('click');
          __doPostBack('<%=HiddenButton.ClientID %>', "");
        });
Community
  • 1
  • 1
Mathematics
  • 7,314
  • 25
  • 77
  • 152
  • If you set the text using javascript this is not possible, you can use a hidden field and set the value of that on form submit with the text content of the button. Then read that value in the code behind. – Jan Jongboom May 02 '13 at 09:55
  • @JanJongboom I added all of code now and logic behind what I am trying to do – Mathematics May 02 '13 at 09:58

1 Answers1

2

You can cast the sender to a button and get the information from that.

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?

Update 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

So when calling __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;

string parameter = Request["__EVENTARGUMENT"];
Community
  • 1
  • 1
Tim B James
  • 20,084
  • 4
  • 73
  • 103
  • 2
    Would `Button btn = sender as Button;` be better in case the cast fails? Then `btn` would end up `null` and you can check that before accessing the `text` property. Otherwise I think the cast could throw an exception if it fails, if I remember correctly. – Nope May 02 '13 at 09:43
  • @FrançoisWahl Yes you could, but since we know that it is a Button, you can just cast it this way. If the `Button` could potentially change to a `LinkButton` or an `ImageButton` in the future then `sender as Button` would safeguard against exceptions. However, you would still have to recode the function. – Tim B James May 02 '13 at 09:47
  • @user13814 You have not set the `Text` property in the code so it is empty. ` ` – Tim B James May 02 '13 at 09:48
  • @TimBJames I am setting it's text using JS code, wait I add more code. – Mathematics May 02 '13 at 09:48
  • Could you test this by just adding the code `__doPostBack('[element name attribute]', '[your argument]');` to your `$(function(){ ... });` call. Make sure that it is the `name` attribute of the Button rather than the ID` – Tim B James May 02 '13 at 10:51