Results 1 to 4 of 4

Thread: [RESOLVED] How to use If in DetailsView TemplateField DropDownList

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2010
    Posts
    28

    Resolved [RESOLVED] How to use If in DetailsView TemplateField DropDownList

    I have a switch.

    When the DetailsView first appears for initial data entry the switch is already set.

    If the switch is set to ‘A’ I want to populate the DetailsView TemplateField DropDownList from one SQL table.

    If the switch is set to ‘B’ I want to populate the DetailsView TemplateField DropDownList from a different SQL table.

    All interest and help is appreciated.

  2. #2
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: How to use If in DetailsView TemplateField DropDownList

    I have a switch.

    When the DetailsView first appears for initial data entry the switch is already set.
    Is switch a checkbox control or something else? Maybe you could show us the DetailsView markup.
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2010
    Posts
    28

    Re: How to use If in DetailsView TemplateField DropDownList

    If the switch is set to 'SCANG', I want to use SqlDataSource3 to get the Air Force rank. If set to 'SCARNG' I want to use SqlDataSource4 for the Army rank. It would be great if I could just set the ddlRank DataSourceId "on the fly".

    Display code:
    Code:
        <div style="text-align:center">
            <br />
            <asp:MultiView ID="MultiView1" runat="server">
                <asp:View ID="View1" runat="server">
                    Select Air or Army Branch:&nbsp;
                    <asp:DropDownList ID="ddlBranch" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlBranch_Selected">
                        <asp:ListItem Text="" Selected="True"></asp:ListItem>
                        <asp:ListItem Text="Air"></asp:ListItem>
                        <asp:ListItem Text="Army"></asp:ListItem>
                    </asp:DropDownList>
                </asp:View>
                <asp:View ID="View2" runat="server">
                    <asp:DetailsView ID="DetailsView1" runat="server" Height="100%" Width="50%" DataSourceID="SqlDataSource1" DefaultMode="Insert"
                        HorizontalAlign="Center" DataKeyNames="SSN" AutoGenerateRows="False" EnableModelValidation="True" OnItemInserting="Record_Inserting" >
                        <CommandRowStyle HorizontalAlign="Center" />
                        <Fields>
                            <asp:TemplateField HeaderText="Social Security Number" SortExpression="SSN">
                                <InsertItemTemplate>
                            <asp:BoundField DataField="NAMEL" HeaderText="Name Last" SortExpression="NAMEL" />
                            <asp:BoundField DataField="NAMEF" HeaderText="Name First" SortExpression="NAMEF" />
                            <asp:BoundField DataField="NAMEM" HeaderText="Name Middle" SortExpression="NAMEM" />
                            <asp:BoundField DataField="ADDR" HeaderText="Address" SortExpression="ADDR" />
                            <asp:BoundField DataField="ADDR2" HeaderText="Address 2" SortExpression="ADDR2" />
                            <asp:BoundField DataField="CITY" HeaderText="City" SortExpression="CITY" />
                            <asp:TemplateField HeaderText="State" SortExpression="STATE">
                                <InsertItemTemplate>
                                    <asp:DropDownList ID="ddlState" runat="server" Text='<%# Bind("STATE") %>' 
                                        DataSourceID="SqlDataSource2" DataTextField="State" DataValueField="Abbreviation" >
                                    </asp:DropDownList>
                                </InsertItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblState" runat="server"  Text='<%# Bind("STATE") %>'>
                                    </asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField DataField="ZIP" HeaderText="Zip Code" SortExpression="ZIP" />
                            <asp:BoundField DataField="EMAIL" HeaderText="E-Mail Address" SortExpression="EMAIL" />
                            <asp:BoundField DataField="DOB" HeaderText="Date of Birth" SortExpression="DOB" ApplyFormatInEditMode="True" DataFormatString="{0:d}" />
                            <asp:BoundField DataField="DISCHDT" HeaderText="Discharge Date" SortExpression="DISCHDT" ApplyFormatInEditMode="True" DataFormatString="{0:d}" />
                            <asp:TemplateField HeaderText="Branch / Component" SortExpression="COMPONENT" ControlStyle-BackColor="LightGray">
                                <InsertItemTemplate>
                                    <asp:TextBox ID="txtCmpt" runat="server" Text='<%# Bind("COMPONENT") %>' ReadOnly="true"></asp:TextBox>
                                </InsertItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblCmpt" runat="server" Text='<%# Bind("COMPONENT") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="RANK" SortExpression="RANK">
                                <InsertItemTemplate>
                                    <asp:DropDownList ID="ddlRANK" runat="server" Text='<%# Bind("RANK")%>'
                                        DataValueField="Abbreviation" DataTextField="Rank" DataSourceId = "SqlDataSource3"></asp:DropDownList>
                                </InsertItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblRANK" runat="server"  Text='<%# Bind("RANK")%>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Edit Date" SortExpression="EDITDT" ControlStyle-BackColor="LightGray">
                                <InsertItemTemplate>
                                    <asp:TextBox ID="txtEditDt" runat="server" Text='<%# Bind("EDITDT", "{0:d}") %>' ReadOnly="true"></asp:TextBox>
                                </InsertItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblEditDt" runat="server" Text='<%# Bind("EDITDT", "{0:d}") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Edit User ID" SortExpression="EDITUSERID" ControlStyle-BackColor="LightGray">
                                <InsertItemTemplate>
                                    <asp:TextBox ID="txtEditID" runat="server" Text='<%# Bind("EDITUSERID") %>' ReadOnly="true"></asp:TextBox>
                                </InsertItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblEditID" runat="server" Text='<%# Bind("EDITUSERID") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:CommandField ShowInsertButton="True" />
                        </Fields>
                        <RowStyle HorizontalAlign="Left" />
                    </asp:DetailsView>
                </asp:View>
            </asp:MultiView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:connString %>" 
                SelectCommand="SELECT * FROM [Pensions]" 
                InsertCommand="INSERT INTO [Pensions] ([NAMEL], [NAMEF], [NAMEM], [ADDR], [ADDR2], [CITY], [STATE], [ZIP], [EMAIL], [DOB], [DISCHDT], [COMPONENT], [RANK], [EDITDT], [EDITUSERID]) VALUES (@NAMEL, @NAMEF, @NAMEM, @ADDR, @ADDR2, @CITY, @STATE, @ZIP, @EMAIL, @DOB, @DISCHDT, @COMPONENT, @RANK, @EDITDT, @EDITUSERID)" >
                <InsertParameters>
                    <asp:Parameter Name="NAMEL" Type="String" />
                    <asp:Parameter Name="NAMEF" Type="String" />
                    <asp:Parameter Name="NAMEM" Type="String" />
                    <asp:Parameter Name="ADDR" Type="String" />
                    <asp:Parameter Name="ADDR2" Type="String" />
                    <asp:Parameter Name="CITY" Type="String" />
                    <asp:Parameter Name="STATE" Type="String" />
                    <asp:Parameter Name="ZIP" Type="String" />
                    <asp:Parameter Name="EMAIL" Type="String" />
                    <asp:Parameter Name="DOB" Type="String" />
                    <asp:Parameter Name="DISCHDT" Type="String" />
                    <asp:Parameter Name="COMPONENT" Type="String" />
                    <asp:Parameter Name="RANK" Type="String" />
                    <asp:Parameter Name="EDITDT" Type="DateTime" />
                    <asp:Parameter Name="EDITUSERID" Type="String" />
                </InsertParameters>
            </asp:SqlDataSource>
            <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                ConnectionString="<%$ ConnectionStrings:connString %>" 
                SelectCommand="SELECT * FROM [States]" >
            </asp:SqlDataSource>
            <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
                ConnectionString="<%$ ConnectionStrings:connString %>" 
                SelectCommand="SELECT * FROM [AirRank]" >
            </asp:SqlDataSource>
            <asp:SqlDataSource ID="SqlDataSource4" runat="server" 
                ConnectionString="<%$ ConnectionStrings:connString %>" 
                SelectCommand="SELECT * FROM [ArmyRank]" >
            </asp:SqlDataSource>
        </div>
    Code behind:
    Code:
    Imports System
    Imports System.Data
    Imports System.Data.SqlClient
    
    Partial Class _01DataEntry
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            If Not IsPostBack Then
                MultiView1.SetActiveView(View1)
            End If
    
        End Sub
    
        Protected Sub ddlBranch_Selected(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlBranch.SelectedIndexChanged
    
            Dim sBranch As String = ddlBranch.SelectedItem.Text
            Dim txtComp As TextBox = DetailsView1.FindControl("txtCmpt")
            Dim txtEditDat As TextBox = DetailsView1.FindControl("txtEditDt")
            Dim txtEditIDent As TextBox = DetailsView1.FindControl("txtEditID")
    
            If sBranch <> "" Then
                If sBranch = "Air" Then
                    txtComp.Text = "SCANG"
                Else
                    txtComp.Text = "SCARNG"
                End If
                txtEditDat.Text = Date.Now()
                txtEditIDent.Text = User.Identity.Name
                MultiView1.SetActiveView(View2)
            End If
    
        End Sub
    
        Protected Sub Record_Inserted(ByVal sender As Object, ByVal e As DetailsViewInsertedEventArgs) Handles DetailsView1.ItemInserted
    
            Response.Redirect("01DataEntry.aspx")
    
        End Sub
    
    End Class
    Last edited by tagtech; Apr 3rd, 2017 at 01:43 PM.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Dec 2010
    Posts
    28

    Re: How to use If in DetailsView TemplateField DropDownList

    I removed the 'Text=(...Bind...)', 'DataSourceID', 'DataTextField' and 'DataValueField' from the DetailsView DropDownList. I then 'hard-coded' a 'Save' to the 'Value' for the SQL data record field in the DetailsView1.ItemInserting routine and now the selected display entry from the DropDownList is saved to the SQL record.

    Code:
        Protected Sub ddlBranch_Selected(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlBranch.SelectedIndexChanged
    
            Dim sBranch As String = ddlBranch.SelectedItem.Text
            Dim txtComp As TextBox = DetailsView1.FindControl("txtCmpt")
            Dim ddlRnk As DropDownList = DetailsView1.FindControl("ddlRank")
            Dim txtEditDat As TextBox = DetailsView1.FindControl("txtEditDt")
            Dim txtEditIDent As TextBox = DetailsView1.FindControl("txtEditID")
    
            If sBranch <> "" Then
                If sBranch = "Air" Then
                    txtComp.Text = "SCANG"
                    ddlRnk.DataSourceID = "SqlDataSource3"
                Else
                    txtComp.Text = "SCARNG"
                    ddlRnk.DataSourceID = "SqlDataSource4"
                End If
                ddlRnk.DataValueField = "Abbreviation"
                ddlRnk.DataTextField = "Rank"
                ddlRnk.DataBind()
                txtEditDat.Text = Date.Now()
                txtEditIDent.Text = User.Identity.Name
                MultiView1.SetActiveView(View2)
            End If
    
        End Sub
    
        Protected Sub Record_Inserting(ByVal sender As Object, ByVal e As DetailsViewInsertEventArgs) Handles DetailsView1.ItemInserting
    
            Dim ddlRnk As DropDownList = DetailsView1.FindControl("ddlRank")
    
            e.Values("Rank") = ddlRnk.Text
    
        End Sub
    Last edited by tagtech; Apr 6th, 2017 at 04:26 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width