Solved

asp.net vb assign dropdownlist selected value based on guid

Posted on 2011-03-08
3
928 Views
Last Modified: 2012-05-11
hello,

i have a listview on a page that gets is value from a LINQ datasource(which in turn gets is record based on a supplied query string).  There are a number of dropdownlists in the item template of the listview, I can successfully assign the values of the Non Bound dropdownlists, but the one dropdownlist I have that is databound is not taking an assigned selected value.  I need help assigning a selected value to the bound dropdownlist

code:
 
<asp:ListView ID="ListView1" runat="server" DataKeyNames="idTrade" 
            DataSourceID="LinqDataTrade">
            <ItemTemplate>
                <asp:Label ID="idTrade" runat="server" Text='<%# Eval("idTrade") %>' Visible="False" />

                <div class="HeadingTxt">                           
                    <asp:Label ID="chrTitleLabel" runat="server" 
                        Text="Update Union Shift Information:"></asp:Label>
                </div> 
                <asp:ValidationSummary ID="ValidationSummary1" runat="server"  ValidationGroup="UpdateTrade" />
                <br />
                <br />
                <div class="TradeLable">Date of Union Shift: (yyyy-mm-dd)
                    <asp:RequiredFieldValidator ID="rfvDate" runat="server" ErrorMessage="Please Enter a Valid Date" SetFocusOnError="True" ValidationGroup="UpdateTrade" ControlToValidate="dtTradeTextBox" Text="*"></asp:RequiredFieldValidator>
                    <asp:CustomValidator ID="cvDate" runat="server" ErrorMessage="Please Enter a Valid Date" SetFocusOnError="True" ValidationGroup="UpdateTrade" ControlToValidate="dtTradeTextBox" Text="*"></asp:CustomValidator>
                <br /></div>
                <asp:TextBox ID="dtTradeTextBox" runat="server" Text='<%# Bind("dtTrade") %>'></asp:TextBox>
                <br />
                <br />
                <div class="TradeLable">Platoon:<br /></div>
                <asp:DropDownList ID="ddlPlatoon" runat="server" >
                    <asp:ListItem Selected="True" Text="1" Value="1"></asp:ListItem>
                    <asp:ListItem Text="2" Value="2"></asp:ListItem>
                    <asp:ListItem Text="3" Value="3"></asp:ListItem>
                    <asp:ListItem Text="4" Value="4"></asp:ListItem>
                </asp:DropDownList>
                <br />
                <br />
                <div class="TradeLable">Day/Night Shift:<br /></div> 
                <asp:DropDownList ID="ddlDayNight" runat="server">
                    <asp:ListItem Selected="True" Text="Day" Value="1"></asp:ListItem>
                    <asp:ListItem Text="Night" Value="2"></asp:ListItem>
                </asp:DropDownList>
                <br />
                <br />
                <div class="TradeLable">Full/Half Shift:<br /></div> 
                <div class="greyText">
                    This indicates if an entire or partial shift is needed to cover the shift. 
                    Please enter needed start or end times in the Shift Details.
                </div>
                <asp:DropDownList ID="ddlHalf" runat="server">
                    <asp:ListItem Selected="True" Text="Full Shift" Value="1"></asp:ListItem>
                    <asp:ListItem Text="Half Shift" Value="2"></asp:ListItem>
                </asp:DropDownList>
                <br />
                <br />
                <div class="TradeLable">Hall:<br /></div>
                <asp:DropDownList ID="ddlHall" runat="server" >
                    <asp:ListItem Selected="True" Text="1" Value="1"></asp:ListItem>
                    <asp:ListItem Text="2" Value="2"></asp:ListItem>
                    <asp:ListItem Text="3" Value="3"></asp:ListItem>
                    <asp:ListItem Text="4" Value="4"></asp:ListItem>
                    <asp:ListItem Text="5" Value="5"></asp:ListItem>
                    <asp:ListItem Text="6" Value="6"></asp:ListItem>
                    <asp:ListItem Text="7" Value="7"></asp:ListItem>
                    <asp:ListItem Text="8" Value="8"></asp:ListItem>
                    <asp:ListItem Text="9" Value="9"></asp:ListItem>
                    <asp:ListItem Text="10" Value="10"></asp:ListItem>
                    <asp:ListItem Text="11" Value="11"></asp:ListItem>
                    <asp:ListItem Text="12" Value="12"></asp:ListItem>
                    <asp:ListItem Text="13" Value="13"></asp:ListItem>
                    <asp:ListItem Text="14" Value="14"></asp:ListItem>
                    <asp:ListItem Text="15" Value="15"></asp:ListItem>
                    <asp:ListItem Text="16" Value="16"></asp:ListItem>
                    <asp:ListItem Text="17" Value="17"></asp:ListItem>
                    <asp:ListItem Text="18" Value="18"></asp:ListItem>
                    <asp:ListItem Text="19" Value="19"></asp:ListItem>
                    <asp:ListItem Text="20" Value="20"></asp:ListItem>
                    <asp:ListItem Text="22" Value="22"></asp:ListItem>
                    <asp:ListItem Text="23" Value="23"></asp:ListItem>
                    <asp:ListItem Text="24" Value="24"></asp:ListItem>
                    <asp:ListItem Text="25" Value="25"></asp:ListItem>
                    <asp:ListItem Text="26" Value="26"></asp:ListItem>
                </asp:DropDownList>
                <br />
                <br />
                <div class="TradeLable">Shift Status:<br /></div>
                <div class="greyText">
                    Active = Shift is availalble to be booked by a member<br />
                    Reserved = Shift is booked by a member and is awating an email confirmation 
                    Confirmed = Shift is booked and confirmed as is now considered complete
                </div>
                <asp:DropDownList ID="ddlStatus" runat="server" >
                    <asp:ListItem Selected="True" Text="Active" Value="1"></asp:ListItem>
                    <asp:ListItem Text="Reserved" Value="2"></asp:ListItem>
                    <asp:ListItem Text="Confirmed" Value="3"></asp:ListItem>
                </asp:DropDownList>
                <br />
                <br />
                <div class="TradeLable">Shift Availability - Private or Public:
                    <asp:CustomValidator ID="cvPrivate" runat="server" ErrorMessage="Please Choose a Union Member" SetFocusOnError="true" ValidateEmptyText="True" ControlToValidate="ddlPrivate" ValidationGroup="UpdateTrade" Text="*"></asp:CustomValidator>
                <br /></div> 
                <div class="greyText">
                    Public Shifts are avilable to all members. Private shifts are only available to 
                    one specified member.</div>
                <asp:DropDownList ID="ddlPrivate" runat="server" >
                    <asp:ListItem Selected="True" Text="Public" Value="1"></asp:ListItem>
                    <asp:ListItem Text="Private" Value="2"></asp:ListItem>
                </asp:DropDownList>
                <br />
                <br />
                <div class="TradeLable">Shift Owner:<br /></div> 
                <div class="greyText">This is the executive or member who needs the shift covered</div>
                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("chrOwner") %>' Width="150px"></asp:TextBox>
                <br />
                <br />
                <div class="TradeLable">Specified Member:<br /></div> 
                <div class="greyText">
                    If shift is private, Select the member from the list who is going to take the 
                    shift. Once the insert button in clicked, they will recieve an email that they 
                    will need to open and confirm in order for the shift to get confirmed. This 
                    union shift will be marked &quot;Reserved&quot; when it is inserted if a memeber has been 
                    selected. Otherwise do not select a member, leave it marked &quot;Available to All.&quot;</div>
                <asp:DropDownList ID="ddlMember" runat="server" AppendDataBoundItems="True" 
                    DataSourceID="LinqDataMember" DataTextField="chrName" DataValueField="UserId" >
                    <asp:ListItem Text="Available to All" Value="0"></asp:ListItem> 
                </asp:DropDownList>            
                <br /><br />
                
                <div class="TradeLable">Reason for Shift:<br /></div> 
                <div class="greyText">
                    Used for Administrative tracking purposes. This information is not seen by the 
                    membership.</div>                
                <asp:TextBox ID="TextBox3" runat="server" 
                    Text='<%# Bind("chrReason") %>' Width="400px"></asp:TextBox>
                <br />
                <br />
                <div class="TradeLable">Shift Details:<br /></div> 
                <div class="greyText">
                    Used for clarity. This information WILL be seen by the membership. For example, 
                    if it is a half shift, you could put the needed start or end time of the shift 
                    here.</div>                
                <asp:TextBox ID="TextBox4" runat="server" 
                    Text='<%# Bind("chrDetails") %>' TextMode="MultiLine" Width="400px" Height="100px"></asp:TextBox>
                <br />
                <br /> 
                <asp:Button ID="btnUpdate" runat="server" Text="Update" CommandArgument='<%# Eval("idTrade") %>' CommandName="Update" ValidationGroup="UpdateTrade" />           
                <asp:Button ID="btnCancel" runat="server" Text="Cancel" PostBackUrl="~/Executive/Trade.aspx" />

            </ItemTemplate>
            
            <EmptyDataTemplate>
                <div class="ItaliBold">There was no union shift record retrieved.  If you have reached this page in error, use the link above to return to the trade management form.</div>
            </EmptyDataTemplate>
            
            <LayoutTemplate>
                <div ID="itemPlaceholderContainer" runat="server" style="">
                    <span ID="itemPlaceholder" runat="server" />
                </div>
                <div style="">
                </div>
            </LayoutTemplate>
            
            
        </asp:ListView>

Open in new window


background code in vb where I use linq to get one record based on query string and assign appropriate dropdownlist values:
 
Protected Sub ListView1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles ListView1.ItemDataBound

        Dim db As New EFFU_LINQDataContext

        Dim idTrade = New Guid(Request.QueryString("tid").ToString)

        Dim myResults = From myTable In db.Trades Where myTable.idTrade = idTrade

        For Each myRow In myResults
            Dim ddlPlatoon As DropDownList = CType(e.Item.FindControl("ddlPlatoon"), DropDownList)
            Dim ddlDayNight As DropDownList = CType(e.Item.FindControl("ddlDayNight"), DropDownList)
            Dim ddlHalf As DropDownList = CType(e.Item.FindControl("ddlHalf"), DropDownList)
            Dim ddlHall As DropDownList = CType(e.Item.FindControl("ddlHall"), DropDownList)
            Dim ddlStatus As DropDownList = CType(e.Item.FindControl("ddlStatus"), DropDownList)
            Dim ddlPrivate As DropDownList = CType(e.Item.FindControl("ddlPrivate"), DropDownList)
            Dim ddlMember As DropDownList = CType(e.Item.FindControl("ddlMember"), DropDownList)

            ddlPlatoon.SelectedValue = myRow.intPlatoon
            ddlDayNight.SelectedValue = myRow.intDayNight
            ddlHalf.SelectedValue = myRow.intHalf
            ddlHall.SelectedValue = myRow.intHall
            ddlStatus.SelectedValue = myRow.intStatus
            ddlPrivate.SelectedValue = myRow.intPrivate

            Dim myString As String = String.Empty
            Dim myGuid As Guid = Nothing
            Try                
                myGuid = New Guid(myRow.idMember.ToString)
                myString = myGuid.ToString
            Catch ex As Exception
                '' error means value is not GUID, therfore no member selected
                myString = 0
            End Try

            ddlMember.SelectedValue = myString

        Next

    End Sub

Open in new window


The drop down list That I can not assign is "ddlMember".  Does anyone know why this one ddl will not accept asignment of a selected value?

0
Comment
Question by:jordanking
3 Comments
 
LVL 11

Expert Comment

by:srikanthreddyn143
ID: 35074100
Please make sure ddlmember.Datasource has rows and your userid is there in the dropdownlist.
0
 
LVL 1

Author Comment

by:jordanking
ID: 35074594
i am note sure how to test for rows.  When i insert a breakpoint in the ItemDataBound event, it does not trigger when i debug, So i am having trouble stepping through the code.

When the page fully renders the ddlMember has rows with the id.  
0
 
LVL 15

Accepted Solution

by:
rajeeshmca earned 500 total points
ID: 35079210
Hi jordanking,

The issue is - before the dropdown is rendered the selected value property is assigned inside the rowdatabound event...


The solution is before u assign the selected value property of the dropdown, bind the drop down in the rowdatabound event like....

ddlMember.DataBind()
ddlMember.SelectedValue = myString


0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 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