This is a migrated thread and some comments may be shown as answers.

Finding the the control which is inside a RadGrid that caused a postback

1 Answer 78 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Lakshman Venkat
Top achievements
Rank 1
Lakshman Venkat asked on 11 Oct 2010, 07:08 PM

Here is the scenario

I have a RadGrid which contains a GridButtonColumn(I have highlighed in the code below). When the user clicks
then I take them to a separate page for editing. I know I can track this in the ItemCommand Event. But What I
am trying to accomplish is if possible get the cotrol id in the Page_Load so that I can skip all the other page
events and take redirect the user to the edit page.Currenly I am not able to do this since I can't get the
control id which cuased the postback (i this case it was the GridButtoncolumn) So my question is How to get
the Id/Control which caused the postback which is inside the RadGrid. Appreciate your reponse

Here is the code
    

<telerik:RadGrid 
    ID="rdgAccountView" 
    runat="server" 
    AllowPaging="True" 
    AllowSorting="True" 
    AutoGenerateColumns="False"
    OnNeedDataSource="rdgAccountView_OnNeedDataSource"
    OnItemCreated="rdgAccountView_ItemCreated"
    OnItemCommand="rdgAccountView_ItemCommand"
    OnPreRender="rdgAccountView_PreRender" 
    OnLoad="rdgAccountView_OnLoad" 
     OnItemDataBound="rdgAccountView_ItemDataBound"                       
    >
    <ClientSettings>
        <Scrolling AllowScroll="True" UseStaticHeaders="True" ScrollHeight="320"/>
        <Selecting AllowRowSelect="False" />
        <Resizing AllowColumnResize="true" ClipCellContentOnResize="true" />
    </ClientSettings>
    <ExportSettings>
        <Pdf FontType="Subset" PaperSize="Letter" />
        <Excel Format="Html" />
    </ExportSettings>
    <MasterTableView 
        Width="100%" 
        NoDetailRecordsText="No Account Records Found"
        CurrentResetPageIndexAction="SetPageIndexToFirst" 
        Dir="LTR" 
        Frame="Border" 
        TableLayout="Auto"
        DataKeyNames="ParentIdentifier">
       
        <RowIndicatorColumn CurrentFilterFunction="NoFilter" FilterListOptions="VaryByDataType"
            Visible="False">
            <HeaderStyle Width="20px" />
        </RowIndicatorColumn>
        <Columns>
            <telerik:GridButtonColumn ButtonType="ImageButton" ImageUrl="../../App_Themes/Default/DataEditingImages/Edit.gif"
                CommandName="EditAccountView" Text="Edit" UniqueName="EditFromAccountView">
                <HeaderStyle Width="20px" />
            </telerik:GridButtonColumn>
            <telerik:GridBoundColumn DataField="FirstName" Groupable="False" HeaderText="First Name" 
                UniqueName="Full Name" ReadOnly="True" ItemStyle-HorizontalAlign="left" HeaderStyle-Width="124px" 
                HeaderStyle-Wrap="true" AutoPostBackOnFilter="true">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="LastName" Groupable="False" HeaderText="Last Name" 
                UniqueName="Full Name" ReadOnly="True" ItemStyle-HorizontalAlign="left" HeaderStyle-Width="124px" 
                HeaderStyle-Wrap="true" AutoPostBackOnFilter="true">
            </telerik:GridBoundColumn>                               
        </Columns>
          
        <EditFormSettings>
            <EditColumn CurrentFilterFunction="NoFilter" FilterListOptions="VaryByDataType">
            </EditColumn>
            <PopUpSettings ScrollBars="None"></PopUpSettings>
        </EditFormSettings>
    </MasterTableView>
    <ClientSettings AllowColumnsReorder="True">
    </ClientSettings>
</telerik:RadGrid>

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 12 Oct 2010, 08:27 AM
Hello,

You can check for the control which invokes the postback using following code. I hope this will help.

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
 
             str = getPostBackControlName();
             if (str == "gbcEditFromAccountView")
             {
                 // Your code here
             }
        }
    }
 
// Method to get the postback control ID
private string getPostBackControlName()
    {
        Control control = null;
        string ctrlname = Page.Request.Params["__EVENTTARGET"];
  
        if (ctrlname != null && ctrlname != String.Empty)
        {
            control = Page.FindControl(ctrlname);
        }
        // if __EVENTTARGET is null, the control is a button type and we need to 
        // iterate over the form collection to find it 
        else
        {
            string ctrlStr = String.Empty;
  
            Control c = null;
  
            foreach (string ctl in Page.Request.Form)
            {
                if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))
                {
                    ctrlStr = ctl.Substring(0, ctl.Length - 2);
                    c = Page.FindControl(ctrlStr);
                }
                else
                {
                    c = Page.FindControl(ctl);
                }
                if (c is System.Web.UI.WebControls.Button || c is System.Web.UI.WebControls.ImageButton)
                {
                    control = c;
                    break;
                }
            }
        }
        return control.ID;
    }


happy coding.
Shinu.
Tags
Grid
Asked by
Lakshman Venkat
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or