Solved

Database Driven DDL

Posted on 2006-06-15
10
381 Views
Last Modified: 2008-02-01
I have a 2 part question. First of I am new to ASP.Net and I am loving it.

My 1st question is I have a data driven dropdown list. It defaults to the first item as beeing selected and displays the grid. I would like to have nothing selected upon entry to the page but would like to have Select Type to appear as default text.

2nd how do I make sure that data is entered when adding a new record using the built in regular expressions?


<%@ Page Language="VB" %>
<script runat="server">

    Protected Sub DetailsView1_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertedEventArgs)
   
        If (Not e.Exception Is Nothing) Then

            ErrorMessageLabel.Text = "An error occured while entering this record.  Please verify you have entered data in the correct format."
            e.ExceptionHandled = True
        End If
        GridView1.DataBind()
    End Sub

    Protected Sub DetailsView1_ItemUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdatedEventArgs)
   
        GridView1.DataBind()
    End Sub

    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
   
        DetailsView1.ChangeMode(DetailsViewMode.ReadOnly)
    End Sub

    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
   
        DetailsView1.ChangeMode(DetailsViewMode.ReadOnly)
    End Sub

    Protected Sub GridView1_PageIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
   
        DetailsView1.ChangeMode(DetailsViewMode.ReadOnly)
    End Sub

    Protected Sub GridView1_Sorted(ByVal sender As Object, ByVal e As System.EventArgs)
        DetailsView1.ChangeMode(DetailsViewMode.ReadOnly)
    End Sub
   
    Protected Sub GridView1_RowDeleted(ByVal sender As Object, ByVal e As GridViewDeletedEventArgs)
   
        If (Not e.Exception Is Nothing) Then
       
            ErrorMessageLabel.Text = "Failed to DELETE due to foreign key contstraint on the table.  You may only delete rows which have no related records."
            e.ExceptionHandled = True
        End If
    End Sub

    Protected Sub DetailsView1_DataBound(ByVal sender As Object, ByVal e As EventArgs)
   
        If (DetailsView1.CurrentMode = DetailsViewMode.Insert) Then
       
            Dim stateTextBox As TextBox = CType(DetailsView1.Rows(6).Cells(1).Controls(0), TextBox)
            stateTextBox.Text = DropDownList1.SelectedValue
            stateTextBox.Enabled = False
        End If
    End Sub

</script>
<html>
<head id="Head1" runat="server">
  <title>E-Risk Services Inventory</title>
  <link rel="Stylesheet" href="style.css" />
</head>
<body>
  <form id="form1" runat="server">
    <b>Equipment Type:</b>
    <asp:DropDownList ID="DropDownList1" DataSourceID="SqlDataSource2" AutoPostBack="true"
      DataTextField="equipmenttype" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" />
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" SelectCommand="SELECT DISTINCT [equipmenttype] FROM [equipmenttype]"
      ConnectionString="<%$ ConnectionStrings:BamAdminConnectionString %>" />
    <br />
    <br />
        <table cellpadding="0" cellspacing="0">
      <tr>
        <td valign="top" style="width: 600px">
       

          <asp:GridView ID="GridView1" AllowPaging="True" runat="server"
            DataSourceID="SqlDataSource1" DataKeyNames="id"
            AutoGenerateColumns="False" Width="500px" SelectedIndex="0" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnPageIndexChanged="GridView1_PageIndexChanged" OnRowDeleted="GridView1_RowDeleted" OnSorted="GridView1_Sorted" CellPadding="0" CellSpacing="0" GridLines="None">
            <Columns>
              <asp:CommandField ShowSelectButton="True" ShowDeleteButton="True" ButtonType="Image" DeleteImageUrl="~/images/delete.gif" EditImageUrl="~/images/documentedit.gif" NewImageUrl="~/images/add.gif" CancelImageUrl="~/images/delete.gif" SelectImageUrl="~/images/approve.gif" UpdateImageUrl="~/images/moveup.gif" HeaderText="ACTION" >
                  <HeaderStyle Wrap="False" CssClass="headertitle" Font-Size="Small" HorizontalAlign="Left" />
                  <ItemStyle HorizontalAlign="Left" Wrap="False" />
              </asp:CommandField>
              <asp:BoundField DataField="equipmenttype" HeaderText="TYPE" SortExpression="equipmenttype" >
                  <ItemStyle HorizontalAlign="Left" Wrap="False" />
                  <HeaderStyle CssClass="headertitle" Font-Size="Small" HorizontalAlign="Left" Wrap="False" />
              </asp:BoundField>
              <asp:BoundField DataField="make" HeaderText="MAKE" SortExpression="make" >
                  <ItemStyle HorizontalAlign="Left" Wrap="False" />
                  <HeaderStyle CssClass="headertitle" Font-Size="Small" HorizontalAlign="Left" Wrap="False" />
              </asp:BoundField>
              <asp:BoundField DataField="model" HeaderText="MODEL" SortExpression="model" >
                  <ItemStyle HorizontalAlign="Left" Wrap="False" />
                  <HeaderStyle CssClass="headertitle" Font-Size="Small" HorizontalAlign="Left" Wrap="False" />
              </asp:BoundField>
              <asp:BoundField DataField="owner" HeaderText="OWNER" SortExpression="owner" >
                  <ItemStyle HorizontalAlign="Left" Wrap="False" />
                  <HeaderStyle CssClass="headertitle" Font-Size="Small" HorizontalAlign="Left" Wrap="False" />
             
              </asp:BoundField>
            </Columns>
              <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
              <AlternatingRowStyle CssClass="Tdatagray" />
                  <EditRowStyle BackColor="#999999" />
                  <RowStyle CssClass="tdatasilva" />
             
          </asp:GridView>
          <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:BamAdminConnectionString %>"
            SelectCommand="SELECT [id], [equipmenttype], [make], [model], [serial], [owner], [name] FROM [hardwareinventory] WHERE ([equipmenttype] = @equipmenttype)"
            DeleteCommand="DELETE FROM [hardwareinventory] WHERE [id] = @id">
            <SelectParameters>
              <asp:ControlParameter ControlID="DropDownList1" Name="equipmenttype" PropertyName="SelectedValue"
                Type="String" />
            </SelectParameters>
          </asp:SqlDataSource>
          <asp:HyperLink ID="HyperLink1" ImageUrl="~/images/add.gif" Text="New" NavigateUrl="DetailsViewInsert.aspx" runat="server" /><small>new</small>
         <asp:HyperLink ID="HyperLink2" ImageUrl="~/images/delete.gif" Text="Cnacel" NavigateUrl="Details.aspx" runat="server" /><small>cancel</small>
        </td>
        <td valign="top" style="width: 225px">
          <asp:DetailsView AutoGenerateRows="False" DataKeyNames="id" DataSourceID="SqlDataSource3"
            HeaderText="EQUIPMENT DETAILS" CssClass="headertitle" ID="DetailsView1" runat="server" Width="275px" OnItemUpdated="DetailsView1_ItemUpdated" OnItemInserted="DetailsView1_ItemInserted" OnDataBound="DetailsView1_DataBound">
            <Fields>
              <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" SortExpression="id" >
                  <ItemStyle CssClass="box_title" HorizontalAlign="Left" VerticalAlign="Top" Wrap="False" />
                  <HeaderStyle CssClass="tdatagray" HorizontalAlign="Left" VerticalAlign="Top" Wrap="False" />
              </asp:BoundField>
              <asp:BoundField DataField="make" HeaderText="make" SortExpression="make" >
                  <ItemStyle CssClass="box_title" HorizontalAlign="Left" VerticalAlign="Top" Wrap="False" />
                  <HeaderStyle CssClass="tdatagray" HorizontalAlign="Left" VerticalAlign="Top" Wrap="False" />
              </asp:BoundField>
              <asp:BoundField DataField="model" HeaderText="model" SortExpression="model" >
                  <ItemStyle CssClass="box_title" />
                  <HeaderStyle CssClass="tdatagray" />
              </asp:BoundField>
              <asp:BoundField DataField="serial" HeaderText="serial" SortExpression="serial" >
                  <ItemStyle CssClass="box_title" />
                  <HeaderStyle CssClass="tdatagray" />
              </asp:BoundField>
              <asp:BoundField DataField="owner" HeaderText="owner" SortExpression="owner" >
                  <ItemStyle CssClass="box_title" />
                  <HeaderStyle CssClass="tdatagray" />
              </asp:BoundField>
              <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" >
                  <ItemStyle CssClass="box_title" />
                  <HeaderStyle CssClass="tdatagray" />
              </asp:BoundField>
              <asp:BoundField DataField="equipmenttype" HeaderText="type" SortExpression="equipmenttype" >
                  <ItemStyle CssClass="box_title" />
                  <HeaderStyle CssClass="tdatagray" />
              </asp:BoundField>
              <asp:CommandField ButtonType="Image" EditImageUrl="~/images/documentedit.gif" CancelImageUrl="~/images/delete.gif"  InsertImageUrl="~/images/disk.gif" NewImageUrl="~/images/add.gif" UpdateImageUrl="~/images/moveup.gif" HeaderText="Action"
                            ShowEditButton="True" ShowInsertButton="True">
                  <HeaderStyle CssClass="tdata" />
                  <ItemStyle CssClass="tdata" />
              </asp:CommandField>
            </Fields>
          </asp:DetailsView>
          <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:BamAdminConnectionString %>"
            SelectCommand="SELECT [id], [equipmenttype], [make], [model], [serial], [owner], [name] FROM [hardwareinventory] WHERE ([id] = @id)"
            UpdateCommand="UPDATE [hardwareinventory] SET [equipmenttype] = @equipmenttype, [make] = @make, [model] = @model, [serial] = @serial, [owner] = @owner, [name] = @name WHERE [id] = @id"
            InsertCommand="INSERT INTO [hardwareinventory] ([equipmenttype], [make], [model], [serial], [owner], [name]) VALUES (@equipmenttype, @make, @model, @serial, @owner, @name)">
            <SelectParameters>
              <asp:ControlParameter ControlID="GridView1" Name="id" PropertyName="SelectedValue"
                Type="String" />
            </SelectParameters>
            <UpdateParameters>
              <asp:Parameter Name="equipmenttype" Type="String" />
              <asp:Parameter Name="make" Type="String" />
              <asp:Parameter Name="model" Type="String" />
              <asp:Parameter Name="serial" Type="String" />
              <asp:Parameter Name="owner" Type="String" />
              <asp:Parameter Name="name" Type="String" />
               <asp:Parameter Name="id" Type="String" />
            </UpdateParameters>
            <InsertParameters>
              <asp:Parameter Name="make" Type="String" />
              <asp:Parameter Name="model" Type="String" />
              <asp:Parameter Name="serial" Type="String" />
              <asp:Parameter Name="owner" Type="String" />
              <asp:Parameter Name="name" Type="String" />
              <asp:Parameter Name="equipmenttype" Type="String" />
            </InsertParameters>
          </asp:SqlDataSource>
        </td>
      </tr>
    </table>
    <br />
    <asp:Label ID="ErrorMessageLabel" EnableViewState="false" runat="server" />
  </form>
</body>
</html>
0
Comment
Question by:-cr-
  • 5
  • 3
  • 2
10 Comments
 
LVL 27

Expert Comment

by:Sammy
Comment Utility
After you bind your dropdownlist add a line like this. Dropdownlist.Items.Insert(0, New ListItem("Please Select a Value", "0")) '--> just tells the compiler to add a new item at index 0 with text of  "Please Select a Value" and  value of 0

as for the regex expressions, you can find tons of free regex patterns here http://regexlib.com/ just use a custom regex control whatever you want your data to match.

HTH


0
 

Author Comment

by:-cr-
Comment Utility
Not sure exactly where I put this.
0
 
LVL 7

Expert Comment

by:kGenius
Comment Utility
You must provides sammy's code in

   Protected Sub DropDownList1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList.DataBound

Dropdownlist1.Items.Insert(0, New ListItem("Please Select a Value", "0"))

    End Sub
0
 
LVL 27

Expert Comment

by:Sammy
Comment Utility
you can do it either like kGenius pointed or you can use your page load to test
if not isPostback then
me.Dropdownlist1.items.insert(0,New ListItem("Please Select a value","0"))
End if
this way whenever you browse the page you will see "Please Select a value" until you actually select then your selected item will show instead


HTH
0
 

Author Comment

by:-cr-
Comment Utility
I am sure I put this in the wrong spot . No error but no item in the dropdown.

Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    DetailsView1.ChangeMode(DetailsViewMode.ReadOnly)
        If Not IsPostBack Then
            DropDownList1.Items.Insert(0, New ListItem("Please Select a value", "0"))
        End If
    End Sub
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 27

Accepted Solution

by:
Sammy earned 500 total points
Comment Utility
You are correct this is the wrong place for the code:-)
you have 2 options/places where this code should be put

1-in the page_load event like this
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
            DropDownList1.Items.Insert(0, New ListItem("Please Select a value", "0"))
        End If
End Sub
2- in the drop down list databound event like this

 Protected Sub Dropdownlist1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles Dropdownlist1.DataBound
DropDownList1.Items.Insert(0, New ListItem("Please Select a value", "0"))
    End Sub

HTH
0
 
LVL 7

Expert Comment

by:kGenius
Comment Utility
When placing it in SelectedIndexChanged it want to add the listitem only when an item in your list has been selected !
By selecting it, a postback occurs. In that manner no item will be added when you check "If Not Page.IsPostBack", hence it is posted back to select ...

HTH is very correct. Accept his post !
0
 

Author Comment

by:-cr-
Comment Utility
Thanks for your help !! :) something so simple. What a difference from classic asp. I am glad we are making the change over to ASP.NET.
Thanks again.
0
 

Author Comment

by:-cr-
Comment Utility
I will be posting another question in relation to the same code. But I wanted to start new to give points appropriately. Will post question link so you can track, and possibly get the (500) points.
0
 

Author Comment

by:-cr-
Comment Utility
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now