Postback issue - ASP.NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • klaul
    New Member
    • Mar 2008
    • 6

    Postback issue - ASP.NET

    Hi there. I'm having an issue with autopostback, in that it doesn't seem to be working!
    The webform I am using pulls the data from an AccessDataSourc e to populate the gridview, which has Edit and Delete functions. I have also configured it so that I can insert into the gridview using the footer row.
    What I am now trying to do is create two searchable dropdowns in the header of the first two columns (company and reference) that would just return the chosen results, but when the options are clicked, the page just refreshes. Have trawled the internet and various books, but seem unable to find any answer; I think it's the way I am creating the dropdown? Since I've never been able to get a dropdown to filter the gridview.
    I am using VB/.NET 2.0 and Visual Studio 2005. Any help as to why my postback isn't working would be fabulous!

    The .aspx page:
    [code=asp]<%@ Page Language="VB" AutoEventWireup ="true" CodeFile="Defau lt.aspx.vb" Inherits="_Defa ult" enableviewstate ="true" smartnavigation ="false"%>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
    <title>Projec t Management</title>


    <style type="text/css">
    #Select1
    {
    height: 16px;
    width: 32px;
    }

    </style>


    </head>
    <body>
    <form id="form1" runat="server">
    <div style="font-family: Arial, Helvetica, sans-serif" >

    <asp:AccessData Source ID="AccessDataS ource1" runat="server"
    ConflictDetecti on="CompareAllV alues" DataFile="~/App_Data/proman.mdb"
    DeleteCommand=" DELETE FROM [tbl1] WHERE [pmid] = @pmid"
    InsertCommand=" INSERT INTO [tbl1] ([pmcom], [pmref], [pmdate], [pmtime], [pmnotes], [pmclosed], [pmfollowup]) VALUES (?, ?, ?, ?, ?, ?, ?)"
    OldValuesParame terFormatString ="original_{ 0}"
    SelectCommand=" SELECT [pmcom], [pmref], [pmdate], [pmtime], [pmnotes], [pmclosed], [pmfollowup], [pmid] FROM [tbl1]"
    UpdateCommand=" UPDATE [tbl1] SET [pmcom] = @pmcom, [pmref] = @pmref, [pmdate] = @pmdate, [pmtime] = @pmtime, [pmnotes] = @pmnotes, [pmclosed] = @pmclosed, [pmfollowup] = @pmfollowup WHERE [pmid] = @pmid" >

    <DeleteParamete rs>
    <asp:Paramete r Name="original_ pmid" Type="Int32" />
    </DeleteParameter s>
    <UpdateParamete rs>
    <asp:Paramete r Name="pmcom" Type="String" />
    <asp:Paramete r Name="pmref" Type="String" />
    <asp:Paramete r Name="pmdate" Type="DateTime" />
    <asp:Paramete r Name="pmtime" Type="DateTime" />
    <asp:Paramete r Name="pmnotes" Type="String" />
    <asp:Paramete r Name="pmclosed" Type="DateTime" />
    <asp:Paramete r Name="pmfollowu p" Type="String" />
    <asp:Paramete r Name="pmid" Type="Int32" />
    </UpdateParameter s>
    <InsertParamete rs>
    <asp:Paramete r Name="column1" Type="String" />
    <asp:Paramete r Name="column2" Type="String" />
    <asp:Paramete r Name="column3" Type="DateTime" />
    <asp:Paramete r Name="column4" Type="DateTime" />
    <asp:Paramete r Name="column5" Type="String" />
    <asp:Paramete r Name="column6" Type="DateTime" />
    <asp:Paramete r Name="column7" Type="String" />
    <asp:Paramete r Name="column8" Type="Int32" />
    </InsertParameter s>
    </asp:AccessDataS ource>


    <asp:AccessData Source ID="AccessDataS ource2" runat="server"
    DataFile="~/App_Data/proman.mdb"
    SelectCommand=" SELECT DISTINCT [pmcom] FROM [tbl1]"></asp:AccessDataS ource>[/code]continues:[code=asp]


    <asp:AccessData Source ID="AccessDataS ource3" runat="server"
    DataFile="~/App_Data/proman.mdb"
    SelectCommand=" SELECT DISTINCT [pmref] FROM [tbl1]"></asp:AccessDataS ource>
    <h1>
    <font color="#49724B" >Project Management</font><br />


    <asp:GridView ID="GridView1" runat="server" AutoGenerateCol umns="False"
    DataKeyNames="p mid" DataSourceID="A ccessDataSource 1" ShowFooter="Tru e"
    AllowPaging="Tr ue" BackColor="Whit e" BorderColor="#3 36666"
    BorderStyle="Do uble" BorderWidth="3p x" CellPadding="4"
    GridLines="Hori zontal" style="font-size: small">
    <FooterStyle BackColor="Whit e" ForeColor="#333 333" />
    <RowStyle BackColor="Whit e" ForeColor="#333 333" />
    <Columns>


    <asp:TemplateFi eld ShowHeader="Fal se">
    <EditItemTempla te>
    <asp:LinkButt on ID="LinkButton1 " runat="server" CausesValidatio n="True" CommandName="Up date" Text="Update" Font-Bold="True" Font-Underline="Fals e" ForeColor="Medi umSeaGreen"></asp:LinkButton>
    &nbsp;
    <asp:LinkButt on ID="LinkButton2 " runat="server" CausesValidatio n="False" CommandName="Ca ncel" Text="Cancel" Font-Bold="True" Font-Underline="Fals e" ForeColor="Medi umSeaGreen"></asp:LinkButton>
    </EditItemTemplat e>
    <FooterTemplate >
    <asp:LinkButt on ID="btnAdd" runat="server" Font-Bold="True" Font-Underline="Fals e"
    ForeColor="#497 24B" OnClick="btnAdd _Click">Insert</asp:LinkButton>
    </FooterTemplate>
    <ItemTemplate >
    <asp:LinkButt on ID="LinkButton1 " runat="server" CausesValidatio n="False"
    CommandName="Ed it" Text="Edit" Font-Bold="True" Font-Underline="Fals e"
    ForeColor="#497 24B"></asp:LinkButton>
    &nbsp;
    <asp:LinkButt on ID="LinkButton2 " runat="server" CausesValidatio n="False"
    CommandName="De lete" Text="Delete" Font-Bold="True" Font-Underline="Fals e"
    ForeColor="#497 24B"></asp:LinkButton>
    </ItemTemplate>
    </asp:TemplateFie ld>

    <asp:TemplateFi eld HeaderText="Com pany" SortExpression= "pmcom" >
    <EditItemTempla te>
    <asp:DropDownLi st ID="DropDownLis t1" runat="server" Text='<%# Bind("pmcom") %>'>
    <asp:ListItem Value="CEDAR"></asp:ListItem>
    <asp:ListItem Value="EAN"></asp:ListItem>
    <asp:ListItem Value="EFIN"></asp:ListItem>
    <asp:ListItem Value="GBC"></asp:ListItem>
    <asp:ListItem ></asp:ListItem>
    </asp:DropDownLis t>
    </EditItemTemplat e>
    <FooterTemplate >
    <asp:DropDownLi st ID="inspmcom" runat="server" AutoPostBack="F alse">
    <asp:ListItem Value="CEDAR"></asp:ListItem>
    <asp:ListItem Value="EAN"></asp:ListItem>
    <asp:ListItem Value="EFIN"></asp:ListItem>
    <asp:ListItem Value="GBC"></asp:ListItem>
    <asp:ListItem ></asp:ListItem>
    </asp:DropDownLis t>
    </FooterTemplate>
    <HeaderTemplate >
    <asp:LinkButt on ID="pmcomlink" runat="server" ForeColor="Whit e" OnClick="pmcoml ink_Click">Comp any</asp:LinkButton>
    <asp:DropDownLi st ID="pmcomdrop"
    DataTextField=" pmcom"
    AutoPostBack="t rue"
    OnSelectedIndex Changed="pmcomd rop_IndexChange d"
    OnPreRender="Se tValue"
    DataSourceID="A ccessDataSource 2" runat="server"
    Visible="false"/>
    </HeaderTemplate>
    <ItemTemplate >
    <asp:Label ID="Label1" runat="server" Text='<%# Bind("pmcom") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateFie ld>[/code]continues:[code=asp]

    <asp:TemplateFi eld HeaderText="Ref erence" SortExpression= "pmref">
    <EditItemTempla te>
    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("pmref") %>'
    Width="75px"></asp:TextBox>
    </EditItemTemplat e>
    <FooterTemplate >
    <asp:TextBox ID="inspmref" runat="server" Width="75px"></asp:TextBox>
    </FooterTemplate>
    <HeaderTemplate >
    <asp:LinkButt on ID="pmreflink" runat="server" ForeColor="Whit e" OnClick="pmrefl ink_Click">Refe rence</asp:LinkButton>
    <asp:DropDownLi st ID="pmrefdrop" runat="server" AutoPostBack="T rue"
    DataSourceID="A ccessDataSource 3" DataTextField=" pmref"
    DataValueField= "pmref" CausesValidatio n="True" Visible="false" OnSelectedIndex Changed="pmrefd rop_IndexChange d">
    </asp:DropDownLis t>
    </HeaderTemplate>
    <ItemTemplate >
    <asp:Label ID="Label2" runat="server" Text='<%# Bind("pmref") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateFie ld>


    <asp:TemplateFi eld HeaderText="Dat e" SortExpression= "pmdate">
    <EditItemTempla te>
    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("pmdate") %>'
    Width="65px"></asp:TextBox>
    </EditItemTemplat e>
    <FooterTemplate >
    <asp:TextBox ID="inspmdate" runat="server" Width="65px"></asp:TextBox>
    &nbsp;<asp:Butt on ID="btnCal" runat="server" onclick="btnCal _Click" Text="..."
    Width="29px" Height="21px" />
    </FooterTemplate>
    <ItemTemplate >
    <asp:Label ID="Label3" runat="server" Text='<%# Bind("pmdate", "{0:dd/MM/yy}") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateFie ld>


    <asp:TemplateFi eld HeaderText="Tim e" SortExpression= "pmtime">
    <EditItemTempla te>
    <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("pmtime") %>'
    Width="45px"></asp:TextBox>
    </EditItemTemplat e>
    <FooterTemplate >
    <asp:TextBox ID="inspmtime" runat="server" Width="45px"></asp:TextBox>
    </FooterTemplate>
    <ItemTemplate >
    <asp:Label ID="Label4" runat="server" Text='<%# Bind("pmtime", "{0:HH:mm}" ) %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateFie ld>


    <asp:TemplateFi eld HeaderText="Not es" SortExpression= "pmnotes">
    <EditItemTempla te>
    <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("pmnotes") %>'></asp:TextBox>
    </EditItemTemplat e>
    <FooterTemplate >
    <asp:TextBox ID="inspmnotes " runat="server"> </asp:TextBox>
    </FooterTemplate>
    <ItemTemplate >
    <asp:Label ID="Label5" runat="server" Text='<%# Bind("pmnotes") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateFie ld>[/code]continues:[code=asp]


    <asp:TemplateFi eld HeaderText="Clo sed" SortExpression= "pmclosed">
    <EditItemTempla te>
    <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("pmclosed" ) %>'
    Width="65px"></asp:TextBox>
    </EditItemTemplat e>
    <FooterTemplate >
    <asp:TextBox ID="inspmclosed " runat="server" Width="65px"></asp:TextBox>
    <asp:Button ID="btnCal2" runat="server" onclick="btnCal 2_Click" Text="..."
    Width="29px" Height="21px"/>
    </FooterTemplate>
    <ItemTemplate >
    <asp:Label ID="Label6" runat="server" Text='<%# Bind("pmclosed" , "{0:dd/MM/yy}") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateFie ld>


    <asp:TemplateFi eld HeaderText="Fol lowup" SortExpression= "pmfollowup ">
    <EditItemTempla te>
    <asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("pmfollowu p") %>'></asp:TextBox>
    </EditItemTemplat e>
    <FooterTemplate >
    <asp:TextBox ID="inspmfollow up" runat="server"> </asp:TextBox>
    </FooterTemplate>
    <ItemTemplate >
    <asp:Label ID="Label7" runat="server" Text='<%# Bind("pmfollowu p") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateFie ld>


    <asp:TemplateFi eld HeaderText="ID" InsertVisible=" False" SortExpression= "pmid" Visible="False" >
    <EditItemTempla te>
    <asp:Label ID="Label1" runat="server" Text='<%# Eval("pmid") %>'></asp:Label>
    </EditItemTemplat e>
    <FooterTemplate >
    <asp:TextBox ID="inspmid" runat="server" Width="50px"></asp:TextBox>
    </FooterTemplate>
    <ItemTemplate >
    <asp:Label ID="Label8" runat="server" Text='<%# Bind("pmid") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateFie ld>

    </Columns>


    <PagerStyle BackColor="#336 666" ForeColor="Whit e" HorizontalAlign ="Center" />
    <SelectedRowSty le BackColor="#339 966" Font-Bold="True" ForeColor="Whit e" />
    <HeaderStyle BackColor="#336 666" Font-Bold="True" ForeColor="Whit e" />
    </asp:GridView>

    </h1>

    </div>

    <center>
    <asp:Calendar ID="Calendar1" runat="server" BackColor="Whit e" BorderColor="Wh ite"
    Font-Names="Verdana" Font-Size="9pt" ForeColor="Blac k" Height="180px"
    Width="200px" Visible="False" NextPrevFormat= "ShortMonth " BorderWidth="1p x">
    <SelectedDaySty le BackColor="#333 333" ForeColor="Whit e" />
    <TodayDayStyl e BackColor="#CCC CCC" />
    <OtherMonthDayS tyle ForeColor="#999 999" />
    <NextPrevStyl e VerticalAlign=" Bottom" Font-Bold="True" Font-Size="8pt"
    ForeColor="#333 333" />
    <DayHeaderSty le Font-Bold="True" Font-Size="8pt" />
    <TitleStyle BackColor="Whit e" ForeColor="#333 333" Font-Bold="True"
    Font-Size="8pt" BorderColor="Bl ack" BorderWidth="4p x" />
    </asp:Calendar>


    <asp:Calendar ID="Calendar2" runat="server" BackColor="Whit e" BorderColor="Wh ite"
    Font-Names="Verdana" Font-Size="9pt" ForeColor="Blac k" Height="180px"
    Width="200px" Visible="False" NextPrevFormat= "ShortMonth " BorderWidth="1p x">
    <SelectedDaySty le BackColor="#333 333" ForeColor="Whit e" />
    <TodayDayStyl e BackColor="#CCC CCC" />
    <OtherMonthDayS tyle ForeColor="#999 999" />
    <NextPrevStyl e VerticalAlign=" Bottom" Font-Bold="True" Font-Size="8pt"
    ForeColor="#333 333" />
    <DayHeaderSty le Font-Bold="True" Font-Size="8pt" />
    <TitleStyle BackColor="Whit e" Font-Bold="True" Font-Size="8pt"
    ForeColor="#333 333" BorderColor="Bl ack" BorderWidth="4p x" />
    </asp:Calendar>
    </center>
    </form>
    </body>
    </html>[/code]

    and the .aspx.vb page

    [CODE=vbnet]Partial Class _Default
    Inherits System.Web.UI.P age

    Protected Sub btnAdd_Click(By Val sender As Object, ByVal e As EventArgs)
    Dim column1 As DropDownList = TryCast(GridVie w1.FooterRow.Fi ndControl("insp mcom"), DropDownList)
    Dim column2 As TextBox = TryCast(GridVie w1.FooterRow.Fi ndControl("insp mref"), TextBox)
    Dim column3 As TextBox = TryCast(GridVie w1.FooterRow.Fi ndControl("insp mdate"), TextBox)
    Dim column4 As TextBox = TryCast(GridVie w1.FooterRow.Fi ndControl("insp mtime"), TextBox)
    Dim column5 As TextBox = TryCast(GridVie w1.FooterRow.Fi ndControl("insp mnotes"), TextBox)
    Dim column6 As TextBox = TryCast(GridVie w1.FooterRow.Fi ndControl("insp mclosed"), TextBox)
    Dim column7 As TextBox = TryCast(GridVie w1.FooterRow.Fi ndControl("insp mfollowup"), TextBox)
    Dim column8 As TextBox = TryCast(GridVie w1.FooterRow.Fi ndControl("insp mid"), TextBox)

    'insert statement. alsi insert into primary key column but insert statement needs to show
    ' values without primary key
    AccessDataSourc e1.InsertParame ters("column1") .DefaultValue = column1.Selecte dValue
    AccessDataSourc e1.InsertParame ters("column2") .DefaultValue = column2.Text
    AccessDataSourc e1.InsertParame ters("column3") .DefaultValue = column3.Text
    AccessDataSourc e1.InsertParame ters("column4") .DefaultValue = column4.Text
    AccessDataSourc e1.InsertParame ters("column5") .DefaultValue = column5.Text
    AccessDataSourc e1.InsertParame ters("column6") .DefaultValue = column6.Text
    AccessDataSourc e1.InsertParame ters("column7") .DefaultValue = column7.Text
    AccessDataSourc e1.InsertParame ters("column8") .DefaultValue = column8.Text

    AccessDataSourc e1.Insert()
    End Sub


    Protected Sub inspmdate_TextC hanged(ByVal sender As Object, ByVal e As System.EventArg s) _
    Handles GridView1.DataB ound

    Dim inspmdate As TextBox = TryCast(GridVie w1.FooterRow.Fi ndControl("insp mdate"), TextBox)
    If inspmdate IsNot Nothing Then
    inspmdate.Text = DateTime.Today. ToString("dd/MM/yy")
    End If

    Dim inspmtime As TextBox = TryCast(GridVie w1.FooterRow.Fi ndControl("insp mtime"), TextBox)
    If inspmtime IsNot Nothing Then
    inspmtime.Text = DateTime.Now.To String("HH:mm")
    End If

    Dim inspmclosed As TextBox = TryCast(GridVie w1.FooterRow.Fi ndControl("insp mclosed"), TextBox)
    If inspmclosed IsNot Nothing Then
    inspmclosed.Tex t = DateTime.Today. ToString("dd/MM/yy")
    End If

    End Sub

    Protected Sub btnCal_Click(By Val sender As Object, ByVal e As System.EventArg s)
    Dim inspmdate As TextBox = TryCast(GridVie w1.FooterRow.Fi ndControl("insp mdate"), TextBox)
    Dim Calendar1 As Calendar = TryCast(form1.F indControl("Cal endar1"), Calendar)

    Try
    If inspmdate.Text. Trim() <> "" Then
    Calendar1.Selec tedDate = Convert.ToDateT ime(inspmdate.T ext)
    End If
    Catch
    End Try
    'shows the calendar...
    Calendar1.Visib le = True

    End Sub

    Protected Sub Calendar1_Selec tionChanged(ByV al sender As Object, ByVal e As System.EventArg s) Handles Calendar1.Selec tionChanged
    Dim inspmdate As TextBox = TryCast(GridVie w1.FooterRow.Fi ndControl("insp mdate"), TextBox)
    Dim Calendar1 As Calendar = TryCast(form1.F indControl("Cal endar1"), Calendar)
    '...displays the date in the textbox...
    inspmdate.Text = Calendar1.Selec tedDate.ToStrin g()
    '...and hides the calendar again
    Calendar1.Visib le = False

    End Sub

    Protected Sub btnCal2_Click(B yVal sender As Object, ByVal e As System.EventArg s)
    Dim inspmclosed As TextBox = TryCast(GridVie w1.FooterRow.Fi ndControl("insp mclosed"), TextBox)
    Dim Calendar2 As Calendar = TryCast(form1.F indControl("Cal endar2"), Calendar)

    Try
    If inspmclosed.Tex t.Trim() <> "" Then
    Calendar2.Selec tedDate = Convert.ToDateT ime(inspmclosed .Text)
    End If
    Catch
    End Try
    'shows the calendar...
    Calendar2.Visib le = True
    End Sub
    Protected Sub Calendar2_Selec tionChanged(ByV al sender As Object, ByVal e As System.EventArg s) Handles Calendar2.Selec tionChanged
    Dim inspmclosed As TextBox = TryCast(GridVie w1.FooterRow.Fi ndControl("insp mclosed"), TextBox)
    Dim Calendar2 As Calendar = TryCast(form1.F indControl("Cal endar2"), Calendar)
    '...displays the date in the textbox...
    inspmclosed.Tex t = Calendar2.Selec tedDate.ToStrin g()
    '...and hides the calendar again
    Calendar2.Visib le = False

    End Sub

    Protected Sub pmcomlink_Click (ByVal sender As Object, ByVal e As System.EventArg s)
    Dim pmcomlink As LinkButton = TryCast(GridVie w1.HeaderRow.Fi ndControl("pmco mlink"), LinkButton)
    Dim pmcomdrop As DropDownList = TryCast(GridVie w1.HeaderRow.Fi ndControl("pmco mdrop"), DropDownList)
    'shows dropdown when linkbutton clicked
    pmcomdrop.Visib le = True
    pmcomlink.Visib le = False
    End Sub
    Protected Sub pmreflink_Click (ByVal sender As Object, ByVal e As System.EventArg s)
    Dim pmreflink As LinkButton = TryCast(GridVie w1.HeaderRow.Fi ndControl("pmre flink"), LinkButton)
    Dim pmrefdrop As DropDownList = TryCast(GridVie w1.HeaderRow.Fi ndControl("pmre fdrop"), DropDownList)
    'shows dropdown when link button clicked
    pmrefdrop.Visib le = True
    pmreflink.Visib le = False
    End Sub

    Protected Sub pmcomdrop_Index Changed(ByVal sender As Object, ByVal e As System.EventArg s)
    Dim pmcomlink As LinkButton = TryCast(GridVie w1.HeaderRow.Fi ndControl("pmco mlink"), LinkButton)
    Dim pmcomdrop As DropDownList = TryCast(GridVie w1.HeaderRow.Fi ndControl("pmco mdrop"), DropDownList)
    ' hides dropdown when a company is selected and show the linkbutton
    pmcomdrop.Visib le = False
    pmcomlink.Visib le = True


    End Sub

    Protected Sub pmrefdrop_Index Changed(ByVal sender As Object, ByVal e As System.EventArg s)
    Dim pmreflink As LinkButton = TryCast(GridVie w1.HeaderRow.Fi ndControl("pmre flink"), LinkButton)
    Dim pmrefdrop As DropDownList = TryCast(GridVie w1.HeaderRow.Fi ndControl("pmre fdrop"), DropDownList)
    ' hide the dropdown when a reference is selected and show the link button
    pmrefdrop.Visib le = False
    pmreflink.Visib le = True
    End Sub



    Protected Sub SetValue(ByVal sender As Object, ByVal e As EventArgs)

    End Sub


    End Class[/CODE]

    Many thanks!
    Last edited by jhardman; Apr 8 '08, 03:30 PM. Reason: split up excessively long code into sections so it will display, move to .NET forum (ASP forum is for "classic" ASP)
  • balame2004
    New Member
    • Mar 2008
    • 142

    #2
    Hi,

    Declare all the dropdown lists as Static(Shared) and try.

    Eg:

    Shared column1 As DropDownList


    Regards,
    Balaji U

    Comment

    Working...