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

[Solved] Inserting with UserControl

4 Answers 156 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Nick
Top achievements
Rank 1
Nick asked on 27 May 2009, 07:40 PM
Hi,

I am inserting using a custom user control and everything works fine except for getting the SelectedItem.Value from the DropDownList.  It always gets the item at index 0.

Any help would be greatly appreciated.

Thank you,
 Nick

4 Answers, 1 is accepted

Sort by
0
ranjana
Top achievements
Rank 1
answered on 28 May 2009, 05:13 AM
                 use this to insert or same code with <UpdateParameter> to update
            <InsertParameters>
                        <asp:ControlParameter ControlID="ddlAudienceType" Name="AudienceTypeId"
                            PropertyName="SelectedValue" />
                        <asp:ControlParameter ControlID="domainSelect" Name="DomainId"
                            PropertyName="SelectedValue" />
                        <asp:ControlParameter ControlID="languageSelect" Name="LanguageId"
                            PropertyName="SelectedValue" />
                    </InsertParameters>
0
Nick
Top achievements
Rank 1
answered on 28 May 2009, 02:55 PM
Thank you much for your reply.  However, I am trying to do this with out defining a datasource in my aspx file.  The datasource is asigned a DataTable in the code behind.  Here's what I got going:

Here is my grid insert command:

protected void RadGrid1_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        var ctrl = e.Item.FindControl(GridEditFormItem.EditFormUserControlID) as UserControl;

        var indicator = (ctrl.FindControl("dllIndicators") as DropDownList).SelectedItem.Value;
        var num = (ctrl.FindControl("tbxNumerator") as TextBox).Text;
        var den = (ctrl.FindControl("tbxDenominator") as TextBox).Text;
        var typ = (ctrl.FindControl("tbxType") as TextBox).Text;

        var row = dtTable.NewRow();

        row["ID"] = dtTable.Rows.Count + 1;
        row["Indicator"] = indicator;
        row["Num"] = num;
        row["Den"] = den;
        row["Ratio"] = num + "/" + den;
        row["Type"] = typ;

        dtTable.Rows.Add(row);

        RadGrid1.DataSource = dtTable;
        RadGrid1.DataBind();

        lblTesting.Text = indicator;
    }

Here is my grid view defined in the aspx:

<telerik:RadGrid ID="RadGrid1" runat="server" Skin="WebBlue" GridLines="Both"
     AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" Width="95%"
     OnNeedDataSource="RadGrid1_NeedDataSource" OnDeleteCommand="RadGrid1_DeleteCommand"
     OnUpdateCommand="RadGrid1_UpdateCommand"
     OnInsertCommand="RadGrid1_InsertCommand"
     CommandItemSettings-AddNewRecordText="Add New Benchmark"
     onitemcommand="RadGrid1_ItemCommand" >
     
     <PagerStyle Mode="NextPrevAndNumeric"></PagerStyle>  
     <MasterTableView DataKeyNames="ID" GridLines="Both" Width="100%" CommandItemDisplay="Top" >
        <Columns>  
           <telerik:GridEditCommandColumn>  
           </telerik:GridEditCommandColumn>
           <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID" ReadOnly="True" Display="false">   
           </telerik:GridBoundColumn>  
           <telerik:GridBoundColumn DataField="Indicator" ReadOnly="true" HeaderText="Indicator" UniqueName="Indicator">   
           </telerik:GridBoundColumn>  
           <telerik:GridBoundColumn DataField="Num" HeaderText="Num" UniqueName="Num">   
           </telerik:GridBoundColumn>  
           <telerik:GridBoundColumn DataField="Den" HeaderText="Den" UniqueName="Den">   
           </telerik:GridBoundColumn>  
           <telerik:GridBoundColumn DataField="Ratio" HeaderText="Ratio" UniqueName="Ratio">   
           </telerik:GridBoundColumn>  
           <telerik:GridBoundColumn DataField="Type" HeaderText="Type" UniqueName="Type">   
           </telerik:GridBoundColumn>  
           <telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="Delete">   
           </telerik:GridButtonColumn>
        </Columns>
        
        <EditFormSettings EditFormType="WebUserControl" UserControlName="" ColumnNumber="2" CaptionFormatString="Edit details for the Benchmark"  
           CaptionDataField="ID">
           <FormTableItemStyle Wrap="False"></FormTableItemStyle>  
           <FormCaptionStyle CssClass="EditFormHeader"></FormCaptionStyle>  
           <FormMainTableStyle CellSpacing="0" CellPadding="3" Width="100%" />  
           <FormTableStyle GridLines="Horizontal" CellSpacing="0" CellPadding="2" CssClass="module"  
               Height="110px" Width="100%" />  
           <FormTableAlternatingItemStyle Wrap="False"></FormTableAlternatingItemStyle>  
           <FormStyle Width="100%" BackColor="#EEF2EA"></FormStyle>  
           <EditColumn UpdateText="Update Benchmark" UniqueName="EditCommandColumn1" CancelText="Cancel Edit">   
           </EditColumn>  
           <FormTableButtonRowStyle HorizontalAlign="Right" CssClass="EditFormButtonRow"></FormTableButtonRowStyle>  
        </EditFormSettings>
        <ExpandCollapseColumn Visible="False">   
           <HeaderStyle Width="19px"></HeaderStyle>  
       </ExpandCollapseColumn>  
       <RowIndicatorColumn Visible="False">   
           <HeaderStyle Width="20px" />  
       </RowIndicatorColumn>
       
     </MasterTableView>
     
</telerik:RadGrid>

Here is the user control for inserting:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="InsertBenchmark.ascx.cs" Inherits="DesktopModules_GCCIndicatorBenchmarks_InsertBenchmark" %>
<div>
New Benchmark
<hr />
<table>
<tr>
<td width="250">Indicator:</td>
<td>
    <asp:DropDownList ID="ddlIndicators" runat="server" >
    </asp:DropDownList>
</td>
</tr>
<tr>
<td width="250">Numerator:</td>
<td>
    <asp:TextBox ID="tbxNumerator" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td width="250">Denominator:</td>
<td>
    <asp:TextBox ID="tbxDenominator" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td width="250">Type:</td>
<td>
    <asp:TextBox ID="tbxType" runat="server"></asp:TextBox></td>
</tr>
</table>
</div>
<div align="right">
<asp:button id="btnInsert" text="Insert Benchmark" runat="server" CommandName="PerformInsert" Visible='<%# DataItem is Telerik.Web.UI.GridInsertionObject %>'></asp:button>
&nbsp;
<asp:button id="btnCancel" text="Cancel Insert" runat="server" causesvalidation="False" commandname="Cancel"></asp:button>
</div>

Here is the Page_Load of the user control:

protected void Page_Load(object sender, EventArgs e)
    {
        var indicators = new string[] { "3-1", "3-2", "3-3", "3-4", "3-5", "3-6" };
        ddlIndicators.DataSource = indicators;
        ddlIndicators.DataBind();
    }

I'm think the problem might be with how I am binding the DropDownList.

Any help would be greatly appreciated.

Thank you,
Nick







0
Shinu
Top achievements
Rank 2
answered on 29 May 2009, 07:36 AM
Hi Nick,

Try setting the AppendDataBoundItems property of the DropDownList to true. Also try populating the DropDownList in the UserControl as shown below and see if it is working.

CS:
             
            object[] indicators = { new ListItem("3-1""1"), new ListItem("3-2""2"), new ListItem("3-3""3"), new ListItem("3-4""4") }; 
            ddlIndicators.DataSource = indicators; 
            ddlIndicators.DataBind(); 

Shinu
0
Nick
Top achievements
Rank 1
answered on 29 May 2009, 04:56 PM
Shinu,

Thank you for your reply.  It's getting the right value for inserting now.  However, the DropDownList has 2 of each item in it.  I tried wrapping that code in an if( ddlIndicators.Items.Count < 1 ) but still had the same problem.  Do you know how I can fix that?  Or any suggestions that will help me out.

Any help is greatly appreciated.

Thanks,
Nick
Tags
Grid
Asked by
Nick
Top achievements
Rank 1
Answers by
ranjana
Top achievements
Rank 1
Nick
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or