0

I need to click a button that is inside a gridview from the codebehind. I think the best approach will be to create a javascript function in codebehind, something like the second solution i tried below. I will appreciate any help

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName == "Accepted")
        {

            int index = Convert.ToInt32(e.CommandArgument);

            GridViewRow row = GridView1.Rows[index];

       //find button

            Button btnEsc = (Button)row.FindControl("btnEsc");

       //here I would like to simulate a click on this button, so far no luck

        btnEsc.Click(); // this is wrong

         }
      }

I also try this but it doesn't find the button: I dont know how to find the button inside the gridview

           System.Text.StringBuilder sbScript = new System.Text.StringBuilder("");
           sbScript.Append("document.getElementById('btnEsc').click();");
           ScriptManager.RegisterStartupScript(this, GetType(), "ClientScript", sbScript.ToString(), true);
Carlos
  • 377
  • 5
  • 24
  • 1
    do you really need to simulate the click or do you just want to execute something that the button would call if clicked? – Robson Nov 07 '14 at 16:15
  • Robson, i need to simulate the click, because that will open a new lightbox. – Carlos Nov 07 '14 at 16:16
  • 1
    then you just want to open a new lightbox, just call the javascript function that creates the lightbox and you're done http://stackoverflow.com/questions/5731224/calling-javascript-function-from-codebehind or other than the lightbox do you need something else? – Robson Nov 07 '14 at 16:20
  • I have to pass an id which is the "e.CommandArgument" and display it inside the lightbox, Let me explain you what I am trying to accomplish: in the gridview there is a button(See Details) when is clicked the first lightbox opens and displays the all the data from the selected row. In this lightbox there is a Button(Accept offer) when is clicked is gonna perform some operation and then open the second lightbox which it will have the Id of the selected row. Please let me know if you need more clarification. Thank you in advance! – Carlos Nov 07 '14 at 16:27
  • 1
    Why not use javascript/jquery to do this? If you still need to call code-behind, make an ajax call and then open the lightbox on success. I don't see why you need such a convoluted solution when all you want to do is pass data to the lightbox. JS is perfect for this. – Chris Nov 07 '14 at 16:33
  • Chris, would you please help me with the script for this, i am not very familiar with javascript. jquery. thank you in advance – Carlos Nov 07 '14 at 16:36
  • 1
    @Carlos, you can directly put all js code into a function say `abc(arg)`, all js code implies, the JS code written for `btnEsc` `OnClientClick`, and then just use same `RegisterStartupScript` with params. It will work then. – Arindam Nayak Nov 07 '14 at 16:37
  • Arindam Nayak, would you please help me with the script for this, i am not very familiar with javascript. jquery. thank you in advance – Carlos Nov 07 '14 at 16:39
  • 1
    Yep, use jquery. See solution, based on the same question you had then, just for demonstration... – Ziv Weissman Nov 07 '14 at 16:47
  • @Carlos you should update your question with what you actually need being as detailed as you can so people can help you better. post the grid code too – Robson Nov 07 '14 at 17:02
  • but i think you should pretty much use custom templates if you are not already using them – Robson Nov 07 '14 at 17:04
  • Robson, I am trying to use the solution that Arindam Nayak suggested, I will let you know the outcome. So far I am able to pass the param to the javascript function. – Carlos Nov 07 '14 at 17:11
  • Thank you guys, all the solution are good. I really appreciate it! – Carlos Nov 07 '14 at 18:26

2 Answers2

1

When you open the lightbox, pass in the Id to a hidden Field. You can then read it out later. Example from ListView here.

protected void lvProjectServices_ItemCreated(object sender, ListViewItemEventArgs e)
    {

            if (e.Item.DataItem != null)
            {
                Whatever data = (Whatever)e.Item.DataItem;

                PlaceHolder objPlc3 = (PlaceHolder)e.Item.FindControl("phEdit");

                LinkButton link3 = new LinkButton();
                link3.Text = "<i class=\"table-edit\"></i>";
                link3.ID = "lbEditServer" + data.Id.ToString();
                link3.CommandName = "Edit";
                link3.CommandArgument = data.Id.ToString();
                link3.Click += link_Click;
                objPlc3.Controls.Add(link3);

            }
        }


    void link_Click(object sender, EventArgs e)
    {

        LinkButton btn = (LinkButton)sender;

        int Id = int.Parse(btn.CommandArgument.ToString());

        txtProjectServiceId.Value = Id.ToString();

            ScriptManager scriptManager = ScriptManager.GetCurrent(Page);

            if (!scriptManager.IsClientScriptBlockRegistered("openSvcModal"))
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "openSvcModal", "$('select').select2(); $('#editProjectService').modal();", true);
            }
    }
0

Code behind:

public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        btn.Text = "Refreshed";
        if (Request.Params["btnPressed"] != null && Request.Params["btnPressed"] == "true")
        {
            ScriptManager.RegisterStartupScript(Page, typeof(Page), "OpenWindow", string.Format("$('#{0}').click()", btn.ClientID), true);
        }
    }

    protected void btn_Click(object sender, EventArgs e)
    {

        btn.Text = "Not Refreshed";
        lbl.Text = "Not Refreshed";
        System.Threading.Thread.Sleep(1000);
        ////to refresh the page
        Page.Response.Redirect(HttpContext.Current.Request.Url.ToString()+"?btnPressed=true", true);
    }
}

Load jquery in aspx page:

<body>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <form id="form1" runat="server">
    <div>
    <asp:Button runat="server" ID="btn" OnClick="btn_Click" PostBackUrl="/WebForm1.aspx" />
        <asp:Label runat="server" ID="lbl"> </asp:Label>
    </div>
    </form>
</body>

You should really learn jquery, its a great tool. g/l.

Ziv Weissman
  • 4,400
  • 3
  • 28
  • 61