Linkbutton In Grid OnClick event

5 posts, 1 answers
  1. Mark
    Mark avatar
    25 posts
    Member since:
    Feb 2012

    Posted 20 Aug 2012 Link to this post

    OK, I have a radgrid with a GridButtonColumn (below)
    <telerik:RadGrid ID="grdOrderedItems" OnNeedDataSource="OrdItem_NeedDataSource"
        OnItemDataBound="OrdItemDatabound" runat="server" AutoGenerateColumns="False" AllowPaging="False">
        <ClientSettings>
            <Scrolling AllowScroll="True" ScrollHeight="250px" UseStaticHeaders="True"></Scrolling>
        </ClientSettings>
        <MasterTableView NoMasterRecordsText="You have no ordered items att his time.">
            <Columns>
                <telerik:GridBoundColumn HeaderText="Order Number" DataField="OrderNumber"></telerik:GridBoundColumn>
                <telerik:GridBoundColumn HeaderText="Item Number" DataField="ItemNumber"></telerik:GridBoundColumn>
                <telerik:GridBoundColumn HeaderText="Item Name" DataField="ItemName"></telerik:GridBoundColumn>
                <telerik:GridButtonColumn HeaderText="View Custom Print" UniqueName="btnView" DataTextField="CustomID"></telerik:GridButtonColumn>
                <telerik:GridBoundColumn HeaderText="Custom ID" DataField="CustomID" Visible="False"></telerik:GridBoundColumn>
                <telerik:GridBoundColumn HeaderText="Friendly Name" DataField="FriendlyName" Visible="False"></telerik:GridBoundColumn>
                <telerik:GridBoundColumn HeaderText="Ordered By" DataField="OrderedBy"></telerik:GridBoundColumn>
                <telerik:GridBoundColumn HeaderText="Shipped?" DataField="Shipped"></telerik:GridBoundColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

    I want to have an onclick event with a command argument
    I kind of have it - but this line
                         btnView.Attributes.Add("OnClick", "LoadProof(null, null)"); 
    gives me some issues...
    protected void OrdItemDatabound(object sender, GridItemEventArgs e)
    {
        if (e.Item.ItemType == GridItemType.AlternatingItem || e.Item.ItemType == GridItemType.Item)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem itemValue = (GridDataItem)e.Item;
     
                LinkButton btnView = (LinkButton)itemValue["btnView"].Controls[0];
                btnView.Attributes.Add("OnClick", "LoadProof(null, null)");
                btnView.CommandArgument = string.Format("return Click('{0}';", itemValue.GetDataKeyValue("CustomID"));
            }
        }
    }

    I need to fire this block of code:
    protected void LoadProof(object sender, EventArgs e)
    {
        //.... do some stuff
    }

    But I can't figure out how to call the object and arguments - for the onclick:
    btnView.Attributes.Add("OnClick", "LoadProof(null, null)");
    Any suggestions?  I know, it shouldn't be "null, null" - but I can't use "sender, e" - Visual Studio doesn't like it.
  2. Mark
    Mark avatar
    25 posts
    Member since:
    Feb 2012

    Posted 20 Aug 2012 Link to this post

    UPDATE!!!
    I started looking around on the forums and found a few somewhat helpful items.
    I started using an ItemCreated process:
    (I need the command argument for the second function)
    if(e.Item is GridDataItem)
    {
        GridDataItem dataitem = (GridDataItem) e.Item;
        LinkButton btn1 = (LinkButton)dataitem["btnView"].Controls[0];
        btn1.CommandArgument = string.Format("return Click('{0}';", (e.Item as GridDataItem)["CustomID"].Text);
        btn1.Click += new EventHandler(LoadProof);
    }

    calling the same code as before (a little more detail, now):
    protected void LoadProof(object sender, EventArgs e)
    {
        var btnView = (LinkButton)sender;
        var customID = btnView.CommandArgument;
     
        Print_Custom_Item it = (from Print_Custom_Item c in dataContext.Print_Custom_Items where c.CartId != null select c).FirstOrDefault();
        if (it != null)
        {
            var ci = new CustomItem();
            ci.LoadArchive(it.CustomID.ToString(), it.ItemNumber);
            ci.CustomId = it.CustomID.ToString();
            ci.ItemNumber = it.ItemNumber;
            Session["PODItem"] = new List<CustomItem>();
            ((List<CustomItem>)Session["PODItem"]).Add(ci);
        }
     
        CustomItem podItem = (from CustomItem c in (List<CustomItem>)Session["PODItem"] where c.CustomId == customID select c).SingleOrDefault();
        if (podItem != null)
        {
            podItem.path = Server.MapPath(@"\CustomPOD\TempImg\");
            if (podItem.GeneratePDF(true, ((LinkButton)sender).ID == "imglargepreview"))
            {
                imgpreview.Visible = true;
                imgpreview.ImageUrl = @"/CustomPOD/TempImg/" + customID + ".png?t=" + DateTime.Now.Ticks.ToString();
                mpe.Show();
            }
        }
    }


    But nothing happens on click... I really need some help with this.
  3. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 20 Aug 2012 Link to this post

    Hi Mark,

    Try adding the Button Click event in the ItemCreated event and give the CommandArgument in the ItemDataBonud event.

    C#:
    protected void grdOrdererItems_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem dataitem = (GridDataItem)e.Item;
            LinkButton btn1 = (LinkButton)dataitem["btnView"].Controls[0];
            btn1.Click += new EventHandler(btn1_Click);
        }
    }
     
    protected void grdOrderedItems_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem dataitem = (GridDataItem)e.Item;
            LinkButton btn1 = (LinkButton)dataitem["btnView"].Controls[0];
            btn1.CommandArgument = string.Format("return Click('{0}';", (e.Item as GridDataItem)["CustomID"].Text);
        }
    }
     
    void btn1_Click(object sender, EventArgs e)
    {
        var btnView = (LinkButton)sender;
        var customID = btnView.CommandArgument;
    }

    Thanks,
    Shinu.
  4. Mark
    Mark avatar
    25 posts
    Member since:
    Feb 2012

    Posted 21 Aug 2012 Link to this post

    Thanks for the suggestion Shinu, but I get an error that says "Parameters do not match the method signature" - for
    "grdOrdererItems_ItemCreated"


    CS0123: No overload for 'ordItemCreated' matches delegate 'Telerik.Web.UI.GridCommandEventHandler'
    (my naming convention is different than yours, but that shouldn't be an issue)

    Apparently, it doesn't like "object sender, GridItemEventArgs e" for that.

    I changed "GridItemEventArgs" to "GridCommandEventArgs" - as a test.  The error is gone, but the functionality still doesn't do what it is supposed to.

    What do you suggest?

    Essentially, when the user clicks the link button, a window opens up showing a preview of an item that they've ordered in the past - I'm not using a RadWindow, just a window.
    But, I am using the RadAjaxManagerProxy to open it up - and it works on another page, just not from a RadGrid (using an asp.net gridview)
  5. Mark
    Mark avatar
    25 posts
    Member since:
    Feb 2012

    Posted 21 Aug 2012 Link to this post

    Another update!!!
    I figured out why it's not working...  but still haven't fixed the issue - but, rest assured, it's not a Telerik issue.
    Your code works fine.  My code, however - needs work.

    It reaches the Click Event and it fires correctly.... I just need to work on my LINQ expressions for my data to come up.

    Thank you for all your help.
Back to Top