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

[Solved] grid keyColumn is not sent from autopostback on dropdownColumn

3 Answers 174 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Hk
Top achievements
Rank 2
Hk asked on 17 Jul 2009, 07:19 AM
hi telerik,
i have a rad grid with an in data key column (studentId) and a drodown column.
When I click on a row to edit it, item_created is fired and i get the correct record studentId from DataBinder.eval.
I attach selectedIndexChanged method on item_created to the dropdown record from the dropdown column, and set it's autopostback to True.
Now when i change the value on the dropdown, the page postback as expected, and first itemcreated is fired (later my selectedindexchanged event is fired too). the problem is that now the student id is 0! meaning that on edit mode, if the post back occured from an inner control on the selected row, the key is not retreived.
is there a proper way to retreieve the record key?

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 17 Jul 2009, 08:01 AM
Hello,

I tried the scenario and found a way to get key value when changing the selection of dropdown. Give a try with the following code that I tried.

ASPX:
 
<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" AllowSorting="True" 
    DataSourceID="SqlDataSource1" OnItemCreated="RadGrid1_ItemCreated"
    <MasterTableView AutoGenerateColumns="False" DataSourceID="SqlDataSource1" 
        DataKeyNames="ContactTitle"
        <Columns>             
            <telerik:GridEditCommandColumn UniqueName="Edit" EditText="Edit"
            </telerik:GridEditCommandColumn> 
            <telerik:GridDropDownColumn UniqueName="GridDropDownColumn" DropDownControlType="DropDownList" ListValueField="CustomerID" 
                DataSourceID="SqlDataSource1" ListTextField="CustomerID" DataField="CustomerID" 
                HeaderText="GridDropDownColumn"
            </telerik:GridDropDownColumn> 
             . . . 
        </Columns> 
    </MasterTableView> 
</telerik:RadGrid> 

C#:
 
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
    if (e.Item is GridEditableItem && e.Item.IsInEditMode) 
    { 
        GridEditableItem item = (GridEditableItem)e.Item; 
        DropDownList dropdown = (DropDownList)item["GridDropDownColumn"].Controls[0]; 
        dropdown.AutoPostBack = true
        dropdown.SelectedIndexChanged += new EventHandler(dropdown_SelectedIndexChanged); 
    } 
void dropdown_SelectedIndexChanged(object sender, EventArgs e) 
    DropDownList dropdown = (DropDownList)sender; 
    GridEditableItem item = (GridEditableItem)dropdown.NamingContainer; 
    string value = item.GetDataKeyValue("ContactTitle").ToString(); // Get the value 
    Response.Write((item["ContactTitle"].Controls[0] as TextBox).Text); // Access the textbox in editmode and get the text 
    Response.Write(value); 

-Shinu.
0
Hadar Kaufman
Top achievements
Rank 1
answered on 19 Jul 2009, 06:31 AM
Hi Shinu,
Thanks for your answer. The thing is I need the CustomerID on the itemCreated event. When fired from the auto postback of the dropdown, it is not retrieved. The selectedIndexChanged event of the dropdown is too late for that. On the item created I re-define the opened row cells (the record) according to the selected index of the drop down.
0
Hadar Kaufman
Top achievements
Rank 1
answered on 19 Jul 2009, 08:31 AM
Hi Shinu,
I actually found a way to use your solution. I need the customerId in the itemCreated in order to generate custon event args to the selectedIndexChanged method of the dropdown. So, according to your solution, Instead of includint it as part of the eventArgs, I generate it in the selectedIndexChanged as you offered.

So thanks for the help again.

Hadar
Tags
Grid
Asked by
Hk
Top achievements
Rank 2
Answers by
Shinu
Top achievements
Rank 2
Hadar Kaufman
Top achievements
Rank 1
Share this question
or