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

[Solved] Rad combo ClientSelectedIndexChanged

3 Answers 273 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Gajanan
Top achievements
Rank 2
Gajanan asked on 25 Jun 2014, 11:27 AM
I have been trying to set the value of a hidden field by using Javascript and
then access the value from within my C# codebehind. When I run the code that is
copied below, the value that is assigned to hidden field is null

i have a rad combo in rad grid on OnClientSelectedIndexChanged i am setting selected value to hidden field , and i have another rad combo on this rad combo item request event i am trying to get value from hidden field 

I would appreciate any help with the matter. I have spent hours trying to
solve what seems like a very simple problem.


<telerik:GridTemplateColumn DataField="CostEntityName" HeaderText="Cost Entity" AllowFiltering="true" UniqueName="CostEntityName1">
  <ItemTemplate>
     <telerik:RadComboBox ID="rcb_PODetailCostEntity" runat="server" MarkFirstMatch="True"
                         EnableLoadOnDemand="True" Height="140px" ShowMoreResultsBox="True"
                         ItemRequestTimeout="500" OnItemsRequested="rcb_PODetailCostEntity_ItemsRequested"
                        Width="99%" DataTextField="StatusEntityName" DataValueField="StatusEntityID"
                        DropDownWidth="280px" ExpandEffect="pixelate" OnClientSelectedIndexChanged="GetSelectedCostEntityID">
     </telerik:RadComboBox>
  </ItemTemplate>
<ItemStyle Width="5%" />
<HeaderStyle Width="5%" />
</telerik:GridTemplateColumn>  

<telerik:GridTemplateColumn DataField="CostEntityKeyName" HeaderText="Cost Entity Key" AllowFiltering="true" UniqueName="CostEntityKeyName1">
<ItemTemplate>
    <telerik:RadComboBox ID="rcb_PODetailCostEntityKey" runat="server" MarkFirstMatch="True"
             EnableLoadOnDemand="True" Height="140px" ShowMoreResultsBox="True"
             ItemRequestTimeout="500" OnItemsRequested="rcb_PODetailCostEntityKey_ItemsRequested"
             Width="99%" DataTextField="StatusEntityName" DataValueField="StatusEntityID"
             DropDownWidth="280px" ExpandEffect="pixelate">
    </telerik:RadComboBox>
</ItemTemplate>
<ItemStyle Width="7%" />
<HeaderStyle Width="7%" />
</telerik:GridTemplateColumn>  



Jva script function calling on client selected index changed
function GetSelectedCostEntityID(sender, eventArgs)  
                {
                    var combo = $find("<%= rgPODetailsForNonInv.ClientID%>").get_masterTableView().get_dataItems()[0].findControl("rcb_PODetailCostEntity");
                    //alert(eventArgs._item._properties._data.value);

                    document.getElementById("ctl00_MainContentPlaceHolder_HiddenField_PODetailCostEntityID").value = eventArgs._item._properties._data.value;
                    //$(document).ready(function() {
                    //$("<%=  HiddenField_PODetailCostEntityID.ClientID  %>").val(eventArgs._item._properties._data.value);
                    //});
                    //$("<%= HiddenField_PODetailCostEntityID.ClientID %>").val(eventArgs._item._properties._data.value);
                    //alert(document.getElementById("ctl00_MainContentPlaceHolder_HiddenField_PODetailCostEntityID").value);
                    //document.getElementById("ctl00_MainContentPlaceHolder_lbl_PODetailcostEntity").value = eventArgs._item._properties._data.value;
                }   

code behind Item request event for 2nd rad combo
 protected void rcb_PODetailCostEntityKey_ItemsRequested ( object sender, RadComboBoxItemsRequestedEventArgs e ) 
    {
        try
        {
            string PageParameter = null;
            RadComboBox rcb_CostEntityKey = (RadComboBox)sender;
            GridDataItem dataItem = (GridDataItem)rcb_CostEntityKey.NamingContainer;
            RadComboBox rcb_CostEntity = (RadComboBox)dataItem.FindControl("rcb_PODetailCostEntity");
            int CostEntityID =0;
            int.TryParse(HiddenField_PODetailCostEntityID.Value, out CostEntityID);
            if (CostEntityID == GlobalConstants.CONTRACT_ENTITY_ID)
                PageParameter = "contract";
            if (CostEntityID == GlobalConstants.CSSALES_ORDER_ENTITY_ID)
                PageParameter = "cssalesorder";
            if (CostEntityID == GlobalConstants.CONTRACT_ITEM_ENTITY_ID)
                PageParameter = "contractitem";
            if (CostEntityID == GlobalConstants.PROJECT_ENTITY_ID)
                PageParameter = "project";
            if (CostEntityID == GlobalConstants.TRADING_SALES_ENTITY_ID)
                PageParameter = "tradingsales";
            if (CostEntityID == GlobalConstants.MANUFACTURE_ORDER_ENTITY_ID)
                PageParameter = "ManufactureOrder";
            GetIDValues.rcb_dKey_ItemsRequested(sender, e, int.Parse(rcb_CostEntity.SelectedValue), rcb_CostEntityKey, PageParameter);
        }
        catch (Exception ex)
        {
            XITingExceptionProcessor.ProcessException(this, ex);
        }
    }





3 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 27 Jun 2014, 07:03 AM
Hi Gajanan,

You can try storing the value in a Session rather than the HiddenField. Please take a look at the following code snippet.

Add this block of code inside form tag of your aspx page. The code help to enable page methods.
ASPX:
<asp:ScriptManager ID="scriptmanagerMain" runat="server" EnablePageMethods="true" ScriptMode="Release" LoadScriptsBeforeUI="true">
</asp:ScriptManager>

JS:
function GetSelectedCostEntityID(sender, eventArgs) {  
    //gets the selected value of 1st RadComboBox
    var selectedId = sender._value;
   //Using WebMethod at code behind to store the session
    PageMethods.CreateSessionViaJavascript(selectedId);
}

C#:
// I create a WebMethod:
[System.Web.Services.WebMethod]
public static string CreateSessionViaJavascript(string selectedId)
{
    Page objp = new Page();
    objp.Session["SelectedID"] = selectedId;
    return selectedId;
}
//After call the web method to create a session from javascript. The session "SelectedID" will have the value of  sender._value.
 
protected void rcb_PODetailCostEntityKey_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
{
   //Get the value from Session
    string value = Session["SelectedID"].ToString();      
}

Thanks,
Princy
0
Gajanan
Top achievements
Rank 2
answered on 07 Jul 2014, 11:35 AM
Hi Princy ,
Thanks for your reply , it worked , but i didn't understand why i cannot store value in Hidden filed at client side n not accessible on server side,
can you pls explain.
0
Princy
Top achievements
Rank 2
answered on 08 Jul 2014, 07:28 AM
Hi Gajanan,

The hidden field value was empty when accessed in server side because you have set the value from a client side event of a control which does not have AutoPostBack=true(Here RadComboBox). As a result the hiddenfield value is not reached to the server side. If you want to use hidden filed, you can set the AutoPostBack property to true for the first combobox. Another best approach is you could subscribe on the client-side OnClientSelectedIndexChange event of the first combobox and store the selected value in a hidden field. Then you should subscribe on the OnClientItemsRequesting event of the second combobox and pass the value from the hidden field through the context object as demonstrated in this help article here.

Thanks,
Princy
Tags
Grid
Asked by
Gajanan
Top achievements
Rank 2
Answers by
Princy
Top achievements
Rank 2
Gajanan
Top achievements
Rank 2
Share this question
or