Solved

Linqdatasource set values according to dropdownlist selection

Posted on 2010-11-21
12
1,336 Views
Last Modified: 2013-11-11
Hi
I have a gridview that has a dropdown column, and regular text fields.
Both the gridview and dropdown are bound to their own separate linqdatasources.
The dropdown is in the first column.  
When a user selects the dropdown, I need to fill in a couple of the regular text fields in the same row.
I would be willing to unbind, change controls, whatever, in order to get this accomplished.

How can I provide such filtering?
Thanks
############# A TRY IN LINQ ROW INSERTING ################

    Protected Sub grdServiceDetails_RowInserting(ByVal sender As Object, ByVal e As TestWeb.GridViewInsertEventArgs) Handles grdServiceDetails.RowInserting
        Dim s As String = e.NewValues(0).ToString
        Dim d As String = e.NewValues(1).ToString
        Dim b As String = e.NewValues(2).ToString
        Dim c As String = e.NewValues(3).ToString

        Dim ddServ As DropDownList = DirectCast(Me.grdServiceDetails.InsertRow.FindControl("ddServiceOffered"), DropDownList)
        Dim tst As TestWeb.ExtendedGridView = Me.grdServiceDetails

        If ddServ Is Nothing Then

            ddServ = DirectCast(Me.grdServiceDetails.Rows(Me.grdServiceDetails.SelectedIndex).FindControl("ddServiceOffered"), DropDownList)
        End If


        Dim iServOffered As Integer = 0
        If Not ddServ Is Nothing Then
            If ddServ.SelectedIndex > -1 Then
                ' Dim gvRow As GridViewRow = DirectCast(ddServ.Parent.Parent, GridViewRow)

                iServOffered = ddServ.SelectedValue

                Dim dc As New SalonDataClassesDataContext
                Dim u As New SalonUtilities
                Dim sUser As String = u.CheckSessionAndReturnUser
                Dim q = (From a In dc.ServicesLUs Where a.UserID.ToString = sUser _
                         And a.LUServiceID = iServOffered Select a).FirstOrDefault
                If Not q Is Nothing Then
                    e.Row.Cells(2).Text = q.Describe
                    e.Row.Cells(3).Text = q.Price
                End If
            End If

        End If
    End Sub

############### A TRY IN DROPDOWN LIST #############

    Protected Sub ddServiceOffered_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
       
        '      e.Result.GetType()
        Dim ddServ As DropDownList = DirectCast(sender, DropDownList)
        'Dim value As String = Me.grdServiceDetails.Rows(row.RowIndex).Cells(0).Text
        'Session("value") = value
        Dim tst As TestWeb.ExtendedGridView = Me.grdServiceDetails
        Dim row As GridViewRow = DirectCast(ddServ.NamingContainer, GridViewRow)

        Dim txt As TextBox = DirectCast(tst.InsertRow.FindControl("txtDescribe"), TextBox)
        ' If Not txt Is Nothing Then
        Dim iServOffered As Integer = 0
        If Not ddServ Is Nothing Then
            If ddServ.SelectedIndex > -1 Then
                iServOffered = ddServ.SelectedValue
                'ddServ.DataBind
                Dim dc As New SalonDataClassesDataContext
                Dim u As New SalonUtilities
                Dim sUser As String = u.CheckSessionAndReturnUser
                Dim q = (From a In dc.ServicesLUs Where a.UserID.ToString = sUser _
                         And a.LUServiceID = iServOffered Select a).SingleOrDefault
                If Not q Is Nothing Then
                    If tst.InsertRowActive Then
                        tst.EnableViewState = True

                        ' ddServ.DataBind()
                        'If Not txt Is Nothing Then
                        '    txt.Text = q.Describe

                        'End If
                        tst.Rows(-1).Cells(2).Text = q.Describe
                        tst.Rows(0).Cells(3).Text = q.Price
                        'tst.DataBind()
                    End If
                    '  Me.grdServiceDetails.InsertRow.Cells.Item(3).Text = q.Describe
                    'Me.grdServiceDetails.InsertRow.Cells(3).Text = q.Price
                End If
            End If

        End If

        end if
        Dim ddServ As DropDownList = DirectCast(sender, DropDownList)

        'To get the row
        Dim gvRow As GridViewRow = DirectCast(ddServ.Parent.Parent, GridViewRow)
        Dim sPrice As String = ddServ.Items("Price").ToString
        Dim sDesc As String = ddServ.Items("Describe").ToString
        Dim ddlParent As DataControlFieldCell = DirectCast(ddServ.Parent, DataControlFieldCell)
        If ddServ.SelectedItem.Value > -1 Then
            Dim i As Integer = ddServ.SelectedValue
            ''I need the other control to change it's source
            Dim dc As New SalonDataClassesDataContext
            Dim u As New SalonUtilities
            Dim sUser As String = u.CheckSessionAndReturnUser
            Dim q = (From a In dc.ServicesLUs Where a.UserID.ToString = sUser _
                     And a.LUServiceID = i Select a)
            If Not q Is Nothing Then

            End If
        End If


    End Sub

################## ASPX ####################

           <cc1:ExtendedGridView ID="grdServiceDetails" 
                runat="server" AllowInserting="True" DataSourceID="LnqServiceDetails" DataKeyNames="ServiceDetailID,LUServiceID" 
              AutoGenerateColumns="False" AllowPaging="True" AllowSorting="True" 
              AscendingImageUrl="~/SortAsc.gif" BorderColor="Silver" CellPadding="4" 
              DescendingImageUrl="~/SortDesc.gif" ForeColor="#333333" PageSize="7" 
              ShowResultSummary="True" Width="100%" Font-Size="9pt" 
                  OnRowInserting="grdServiceDetails_RowInserting" >
                <PagerSettings Position="topAndBottom" />
                <Columns>
                    <asp:BoundField Visible="false" ControlStyle-CssClass="gridcontrol" 
                        ItemStyle-CssClass="gridrow" DataField="ServiceID" >
                        
                        <ControlStyle CssClass="gridcontrol"></ControlStyle>

                    <ItemStyle CssClass="gridrow"></ItemStyle>
                    </asp:BoundField>
                  <asp:TemplateField ControlStyle-CssClass="gridcontrol" 
                        ItemStyle-CssClass="gridrow" HeaderText="Service Type">
                  <ItemTemplate>
                 <%#Eval("ServicesLU.ServiceOffered")%>
    			</ItemTemplate>	         
              <EditItemTemplate>
                  <gdd:ExtendedDropDownList ID="ddServiceOffered" runat="server" DataSourceID="LnqServicesLU"
                     DataTextField="ServiceOffered" DataValueField="LUServiceID" 
                     OnSelectedIndexChanged="ddServiceOffered_SelectedIndexChanged" AutoPostBack="true"
                     SelectedValue='<%# Bind("LUServiceID") %>' >
                      
                  </gdd:ExtendedDropDownList>
              </EditItemTemplate>
              <InsertItemTemplate>
                  <gdd:ExtendedDropDownList ID="ddServiceOffered" runat="server" DataSourceID="LnqServicesLU"
                     DataTextField="ServiceOffered" DataValueField="LUServiceID" 
                     OnSelectedIndexChanged="ddServiceOffered_SelectedIndexChanged" AutoPostBack="true"
                     SelectedValue='<%# Bind("LUServiceID") %>' >
                      
                  </gdd:ExtendedDropDownList>
             </InsertItemTemplate>
              
              <ControlStyle BorderStyle="None" CssClass="gridcontrol" />

                <ItemStyle CssClass="gridrow" Width="20%"></ItemStyle>
                </asp:TemplateField>
                    <asp:BoundField Visible="true" ControlStyle-CssClass="gridcontrol" 
                        ItemStyle-CssClass="gridrow" DataField="Describe" >
                        
                        <ControlStyle CssClass="gridcontrol"></ControlStyle>

                    <ItemStyle CssClass="gridrow"></ItemStyle>
                    </asp:BoundField>

                
                
                    <asp:BoundField ControlStyle-CssClass="gridcontrol" 
                        ItemStyle-CssClass="gridrow" DataField="Price" HeaderText="Price" 
                        ApplyFormatInEditMode="True" DataFormatString="{0:C}" 
                        >
                    <ControlStyle CssClass="gridcontrol"></ControlStyle>

                    <ItemStyle CssClass="gridrow" Width="10%"></ItemStyle>
                    </asp:BoundField>
                    
                  <asp:TemplateField ControlStyle-CssClass="gridcontrol" 
                        ItemStyle-CssClass="gridrow" HeaderText="Formula">
                  <ItemTemplate>
                 <%#Eval("FormulasLU.FormulaName")%>
    			</ItemTemplate>	         
                      <EditItemTemplate>
                          <gdd:ExtendedDropDownList ID="ddFormula" runat="server" DataSourceID="LnqFormulasLU"
                             DataTextField="FormulaName" DataValueField="FormulaID" 
                             SelectedValue='<%# Bind("FormulaID") %>' >
                              
                          </gdd:ExtendedDropDownList>
                      </EditItemTemplate>
                      <ControlStyle BorderStyle="None" CssClass="gridcontrol" />

                    <ItemStyle CssClass="gridrow" Width="20%"></ItemStyle>
                    </asp:TemplateField>
                    <asp:ButtonField CommandName="FormulaView" Text="View Formula" />
                                     
                    <asp:CommandField HeaderText="Action" ShowDeleteButton="True" 
                        ShowEditButton="True" ShowHeader="True" ShowInsertButton="True">
                        <ControlStyle Font-Size="9pt" />
                    </asp:CommandField>
                </Columns>
                <FooterStyle CssClass="gridheader" />
                <RowStyle BackColor="#f9e6fb"  Font-Size="9pt" Font-Names="Verdana" />
                <EditRowStyle BackColor="#f9e604"  Font-Size="9pt" Font-Names="Verdana" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True"  Font-Size="9pt" ForeColor="#333333" />
                <PagerStyle HorizontalAlign="Center" Font-Size="8pt" Font-Names="Verdana" />
                <HeaderStyle BackColor="Purple" Font-Bold="True"  Font-Size="9pt" ForeColor="White" />
                <AlternatingRowStyle BackColor="White"  Font-Size="9pt" Font-Names="Verdana" />                                  
    			
                </cc1:ExtendedGridView>         
              <asp:LinqDataSource ID="LnqServicesLU" runat="server"
                  ContextTypeName="SalonDataClassesDataContext" OrderBy="ServiceOffered" 
                  TableName="ServicesLUs" Where="UserID == @UserID" OnSelected="LnqServicesLU_Selected">
                  <WhereParameters>
                      <asp:SessionParameter DbType="Guid" Name="UserID" SessionField="UserID" />
                  </WhereParameters>
              </asp:LinqDataSource>
                
              <asp:LinqDataSource ID="LnqFormulasLU" runat="server" 
                  ContextTypeName="SalonDataClassesDataContext" OrderBy="FormulaName" 
                  TableName="FormulasLUs" Where="UserID == @UserID">
                  <WhereParameters>
                      <asp:SessionParameter DbType="Guid" Name="UserID" SessionField="UserID" />
                  </WhereParameters>
              </asp:LinqDataSource>
              
              <asp:LinqDataSource ID="LnqServiceDetails" runat="server" 
                  ContextTypeName="SalonDataClassesDataContext" EnableDelete="True" 
                  EnableInsert="True" EnableUpdate="True" TableName="ServiceDetails" 
                  Where="ServiceID == @ServiceID">
                  <WhereParameters>
                      <asp:ControlParameter ControlID="txtServiceID" Name="ServiceID" 
                          PropertyName="Text" Type="Int32" />
                  </WhereParameters>
              </asp:LinqDataSource>

Open in new window

0
Comment
Question by:Sheritlw
  • 5
  • 4
12 Comments
 
LVL 28

Expert Comment

by:strickdd
ID: 34190961
Create and OnSelectedIndexChanged even on the dropdown. Handle that event by pulling the data you need and updating that row in the GV to set the controls to the proper/new values.
0
 

Author Comment

by:Sheritlw
ID: 34191345
That's exactly what I did in my posted sample code.  I even tried using the linqdatasource inserting event.
Neither fills in the bound fields according the dropdown selection.

Thanks
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34204532
You can use a control parameter with the LINQDataSource that is used for the textboxes.

Check the code example here

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linqdatasource.whereparameters.aspx
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:Sheritlw
ID: 34207459
Hi CodeCruiser,

I attached a screen shot of what I'm trying to do.
I have where parameters on both linqdatasources.
When someone selects an item from the dropdownlist, I need to fill the other cells with default values from the dropdownlists linqdatasource.
The gridview I am using is an extended gridview and the row index is -1, virtually non-existent.
I thought if I could insert the default values into the LinqDataSource from the dropdownlist selectedindexchanged event, then I might be able to figure out a way to get this to work.
If I need to change from the gridview to something else, that is fine.
I appreciate your help.
Thank you
0
 

Author Comment

by:Sheritlw
ID: 34207470
Forgot attachment DropDownList in Grid
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34208062
So you want default values in Description, Price, Qty, and Total columns? Where would the values come from?
0
 

Author Comment

by:Sheritlw
ID: 34209808
The linqdatasource that is bound to the dropdownlist contains the values.

Thanks
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 34211400
Trouble is that the LINQDataSource does not provide a way of retrieving values from the active record(in my knowledge). When you select an item from the dropdown list, does the page postback? If it does, you may have to write code to retrieve the values for selected item and put into the textboxes.
0
 

Author Comment

by:Sheritlw
ID: 34217866
Hi CodeCruiser,

I don't think I'm going to be able to find a solution for this using the standard grid with linq.
I am changing grids and going to use fpSpreads grid.  It will allow me to do what I need to do.
Thank you for your help.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34217889
Hi sheritlw,
Sorry for not being able to fix this for you but glad that you found a work around.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question