2012-05-20 2 views
0

Je veux ajouter le détail du stand dans la base de données, après l'utilisateur, insérer le pseudonyme du stand, sélectionner le type de stand et la durée du stand, cliquer sur le bouton soumettre et le nouvel enregistrement apparaît dans la grille sous les anciens enregistrements.Comment mettre à jour gridview lorsque vous cliquez sur le bouton Soumettre?

enter image description here

Je sais qu'il ya quelque chose qui manque dans le code mais je ne sais pas quel est le problème.

Le code client:

<%@ Page Language="VB" MasterPageFile="~/MasterPageAdmin.master" AutoEventWireup="false" CodeFile="addbooth.aspx.vb" Inherits="addbooth" title="Untitled Page" %> 
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %> 
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> 
</asp:Content> 

<asp:Content ID="ContentPlaceHolder2" runat="server" 
contentplaceholderid="ContentPlaceHolder2"> 

<table class="style8" cellpadding="3"> 
<tr> 
<td class="style9">Add New Booth</td> 
<td>&nbsp;</td> 
</tr> 
<tr> 
<td class="style9">&nbsp;</td> 
<td>&nbsp;</td> 
</tr> 
<tr> 
<td class="style9">Event Title:</td> 
<td> 
<asp:SqlDataSource ID="SqlDataSourceEventTitle" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>" SelectCommand="SELECT DISTINCT [eventID], [eventTitle] FROM [eventinfo]"> 
</asp:SqlDataSource> 
<asp:DropDownList ID="ddlEventTitle" runat="server" DataSourceID="SqlDataSourceEventTitle" DataTextField="eventTitle" DataValueField="eventID" Width="240px" AutoPostBack="True"> 
</asp:DropDownList> 
</td> 
</tr> 
<tr> 
<td class="style9">Booth Alias:</td> 
<td> 
<asp:TextBox ID="txtBoothAlias" runat="server" Width="235px"></asp:TextBox> 
</td> 
</tr> 
<tr> 
<td class="style9"> Booth Type:</td> 
<td> 
<asp:DropDownList ID="ddlBoothType" runat="server" Width="240px" AutoPostBack="True"> 
<asp:ListItem Value="7">8&#39;x8&#39;</asp:ListItem> 
<asp:ListItem Value="10">9&#39;x8&#39;</asp:ListItem> 
<asp:ListItem Value="11">12&#39;x50&#39;</asp:ListItem> 
<asp:ListItem Value="12">16&#39;x10&#39;</asp:ListItem> 
<asp:ListItem Value="13">20&#39;x16&#39;</asp:ListItem> 
<asp:ListItem Value="15">32&#39;x10&#39;</asp:ListItem> 
<asp:ListItem Value="17">32&#39;x14.5&#39;</asp:ListItem> 
</asp:DropDownList> 
</td> 
</tr> 
<tr> 
<td class="style10" valign="top" colspan="2">Booth Duration:<br /> 
<font size="2">Tick to activate availability</font></td> 
</tr> 
<tr> 
<td class="style10" valign="top" colspan="2" align="center"> 
<asp:Panel ID="Panel1" runat="server" Width="328px" Direction="LeftToRight" HorizontalAlign="Center"> 
<table cellpadding="5" class="style8"> 
<tr> 
<td> 
<asp:CheckBox ID="cbWeek1" runat="server" Text="Week 1" TextAlign="Left" OnCheckedChanged="cbWeek1_CheckedChanged" AutoPostBack="true"/> 
    </td> 
    <td> 
<asp:CheckBox ID="cbWeek2" runat="server" Text="Week 2" TextAlign="Left" OnCheckedChanged="cbWeek2_CheckedChanged" AutoPostBack="true"/> 
    </td> 
    <td> 
    <asp:CheckBox ID="cbWeek3" runat="server" Text="Week 3" TextAlign="Left" OnCheckedChanged="cbWeek3_CheckedChanged" AutoPostBack="true"/> 
    </td> 
    <td> 
<asp:CheckBox ID="cbWeek4" runat="server" Text="Week 4" TextAlign="Left" OnCheckedChanged="cbWeek4_CheckedChanged" AutoPostBack="true"/> 
                       </td> 
                      </tr> 
                      <tr> 
                       <td> 
                          <asp:CheckBox ID="cbWeek5" runat="server" Text="Week 5" TextAlign="Left" OnCheckedChanged="cbWeek5_CheckedChanged" AutoPostBack="true"/> 
                       </td> 
                       <td> 
                           <asp:CheckBox ID="cbWeek6" runat="server" Text="Week 6" TextAlign="Left" OnCheckedChanged="cbWeek6_CheckedChanged" AutoPostBack="true"/> 
                       </td> 
                       <td> 
                           <asp:CheckBox ID="cbWeek7" runat="server" Text="Week 7" TextAlign="Left" OnCheckedChanged="cbWeek7_CheckedChanged" AutoPostBack="true"/> 
                       </td> 
                       <td> 
                           <asp:CheckBox ID="cbWeek8" runat="server" Text="Week 8" TextAlign="Left" OnCheckedChanged="cbWeek8_CheckedChanged" AutoPostBack="true"/> 
                       </td> 
                      </tr> 
                     </table> 

    </asp:Panel> 
    <asp:RoundedCornersExtender ID="Panel1_RoundedCornersExtender"runat="server" Enabled="True" TargetControlID="Panel1" BorderColor="CadetBlue" Color="ButtonFace"> 
</asp:RoundedCornersExtender> 
     </td> 
     </tr> 
     <tr> 
     <td class="style11"> 
     </td> 
     <td class="style11"> 
<asp:Button ID="ButtonAddNewBooth" runat="server" Text="Add New Booth" Height="50px" /> 
</td> 
</tr> 
    <tr> 
    <td class="style9"> &nbsp;</td> 
    <td>&nbsp;</td> 
     </tr> 
     <tr> 
<td class="style9" colspan="2"> 
<asp:GridView ID="GridViewBoothAdded" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSourceBoothAdded" Font-Size="Small" ForeColor="#333333" GridLines="None" AllowPaging="True" PageSize="5" Visible="False"> 
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> 
<Columns> 
    <asp:BoundField DataField="boothAlias" HeaderText="Booth Alias" SortExpression="boothAlias" /> 
<asp:BoundField DataField="week1" HeaderText="week1" SortExpression="week1" /> 
<asp:BoundField DataField="week2" HeaderText="week2" SortExpression="week2" /> 
<asp:BoundField DataField="week3" HeaderText="week3" SortExpression="week3" /> 
<asp:BoundField DataField="week4" HeaderText="week4" SortExpression="week4" /> 
<asp:BoundField DataField="week5" HeaderText="week5" SortExpression="week5" /> 
<asp:BoundField DataField="week6" HeaderText="week6" SortExpression="week6" /> 
<asp:BoundField DataField="week7" HeaderText="week7" SortExpression="week7" /> 
<asp:BoundField DataField="week8" HeaderText="week8" SortExpression="week8" /> 
    </Columns> 
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> 
    <EmptyDataTemplate> 
     No booth record 
    </EmptyDataTemplate> 
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> 
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" Font-Size="Small" ForeColor="White" /> 
    <EditRowStyle BackColor="#999999" /> 
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
    </asp:GridView> 
    <asp:RoundedCornersExtender ID="GridViewBoothAdded_RoundedCornersExtender" runat="server" Enabled="True" TargetControlID="GridViewBoothAdded"> 
    </asp:RoundedCornersExtender> 
    <asp:SqlDataSource ID="SqlDataSourceBoothAdded" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>" 
    SelectCommand="SELECT [week1], [week2], [week3], [week4], [week5], [week6], [week7], [week8], [eventID], [boothAlias], [eventTitle] FROM [booth_eventinfo] WHERE ([eventID] = @eventID)"> 
    <SelectParameters> 
    <asp:ControlParameter ControlID="ddlEventTitle" Name="eventID" PropertyName="SelectedValue" Type="Int32" /> 
    </SelectParameters> 
    </asp:SqlDataSource> 
                   </td> 
                  </tr> 
    </table> 

code serveur:

Imports System 
Imports System.Data 
Imports System.Data.SqlClient 
Imports System.Threading 
Partial Class addbooth 
Inherits System.Web.UI.Page 

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    GridViewBoothAdded.Visible = True 
End Sub 

Protected Sub ButtonAddNewBooth_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonAddNewBooth.Click 
    Dim connString As String = ConfigurationManager.ConnectionStrings("ConnectionString1").ConnectionString 
    '"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\eventdb.mdf;Integrated Security=True;User Instance=True" 
    Dim con As SqlConnection = New SqlConnection(connString) 

    Dim cmdQuery As String = "INSERT INTO booth(eventID,boothAlias,boothType,week1,week2,week3,week4,week5,week6,week7,week8) VALUES (@eventID,@boothAlias,@boothType,@week1,@week2,@week3,@week4,@week5,@week6,@week7,@week8)" 

    Dim cmd = New SqlCommand(cmdQuery) 
    cmd.Connection = con 
    cmd.CommandType = CommandType.Text 
    con.Open() 
    cmd.Parameters.AddWithValue("@eventID", ddlEventTitle.SelectedValue) 
    cmd.Parameters.AddWithValue("@boothAlias", txtBoothAlias.Text) 
    cmd.Parameters.AddWithValue("@boothType", ddlBoothType.SelectedValue) 

    If (cbWeek1.Checked = True) Then 
     cmd.Parameters.AddWithValue("@week1", "1") 
    Else 
     cmd.Parameters.AddWithValue("@week1", "0") 
    End If 

    If (cbWeek2.Checked = True) Then 
     cmd.Parameters.AddWithValue("@week2", "1") 
    Else 
     cmd.Parameters.AddWithValue("@week2", "0") 
    End If 

    If (cbWeek3.Checked = True) Then 
     cmd.Parameters.AddWithValue("@week3", "1") 
    Else 
     cmd.Parameters.AddWithValue("@week3", "0") 
    End If 

    If (cbWeek4.Checked = True) Then 
     cmd.Parameters.AddWithValue("@week4", "1") 
    Else 
     cmd.Parameters.AddWithValue("@week4", "0") 
    End If 

    If (cbWeek5.Checked = True) Then 
     cmd.Parameters.AddWithValue("@week5", "1") 
    Else 
     cmd.Parameters.AddWithValue("@week5", "0") 
    End If 

    If (cbWeek6.Checked = True) Then 
     cmd.Parameters.AddWithValue("@week6", "1") 
    Else 
     cmd.Parameters.AddWithValue("@week6", "0") 
    End If 

    If (cbWeek7.Checked = True) Then 
     cmd.Parameters.AddWithValue("@week7", "1") 
    Else 
     cmd.Parameters.AddWithValue("@week7", "0") 
    End If 

    If (cbWeek8.Checked = True) Then 
     cmd.Parameters.AddWithValue("@week8", "1") 
    Else 
     cmd.Parameters.AddWithValue("@week8", "0") 
    End If 

    GridViewBoothAdded.Visible = True 

Répondre

0

Je vois dans votre méthode de ButtonAddNewBooth_Click vous manque le ExecuteSql() pour le code bind sqlcommand et gridview

Après l'ajout de paramètres t o cmd ajouter les deux lignes suivantes un pour exécuter l'insert et l'autre pour reconsolidation la grille

cmd.ExecuteSql() 

GridViewBoothAdded.DataBind() 
0

Pouvez-vous vous déplacer GridView code de liaison de données à code-behind fichier Page_Load Méthode & appel le même code à la fin de votre événement ButtonAddNewBooth_Click. Cela servira votre but.

Lien: GridView.DataBind

Questions connexes