2009-11-21 69 views
2

我有一個gridview,當處於編輯模式,我使用下拉菜單來加載所有省份,但是,當我改變省和點擊更新,我的頁面崩潰.. .I'm不發送全省參數GridView的updateparameters正確...這裏是我的代碼,有人請幫助..傳遞下拉值更新參數爲ASP.Net Gridview

  <asp:GridView runat="server" ID="gvUsers" DataKeyNames="UserID" BackColor="#eeeeee" Width="85%" 
        HorizontalAlign="Center" 
        Font-Bold="False" Font-Names="Verdana" 
        Font-Size="10pt" AutoGenerateColumns="False" 
        OnRowDataBound="gvUsers_RowDataBound" 
        OnRowDeleting="gvUsers_RowDeleting" > 
      <HeaderStyle BackColor="#904601" ForeColor="White" 
        Font-Bold="True" HorizontalAlign="Left" /> 
      <SelectedRowStyle BackColor="Yellow" /> 
      <AlternatingRowStyle BackColor="White" Font-Bold="false" /> 
        <Columns> 
         <asp:TemplateField> 
         <ItemTemplate> 
          <asp:LinkButton ID="LinkButton2" 
          CommandArgument='<%# Eval("UserID") %>' 
          CommandName="Select" runat="server"> 
          Select</asp:LinkButton> 
         </ItemTemplate>  
         </asp:TemplateField> 
         <asp:BoundField DataField="UserID" Visible="False" /> 
         <asp:TemplateField HeaderText="Name"> 
          <ItemTemplate> 
           <asp:Label ID="Label1" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label> 
           <asp:Label ID="Label2" runat="server" Text='<%# Eval("LastName") %>'></asp:Label> 
          </ItemTemplate> 
         </asp:TemplateField> 
         <asp:TemplateField HeaderText="Delete?"> 
         <ItemTemplate> 
          <asp:LinkButton ID="LinkButton1" 
          CommandArgument='<%# Eval("UserID") %>' 
          CommandName="Delete" runat="server"> 
          Delete</asp:LinkButton> 
         </ItemTemplate> 
         </asp:TemplateField> 
        </Columns> 
       <RowStyle HorizontalAlign="Left" /> 
       </asp:GridView><br /><br /> 
       <asp:DetailsView runat="server" ID="dvUser" DataSourceID="SqlDataSource1" AutoGenerateRows="False" Width="85%" DataKeyNames="UserID" 
        HorizontalAlign="Center" AutoGenerateInsertButton="True" AutoGenerateEditButton="True" OnItemInserted="dvUsers_ItemInserted" > 
        <Fields> 
         <asp:BoundField DataField="UserID" Visible="False" /> 
         <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" /> 
         <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" /> 
         <asp:BoundField DataField="UserName" HeaderText="UserName" ReadOnly="true" SortExpression="UserName" /> 
         <asp:TemplateField HeaderText="Password"> 
         <ItemTemplate> 
          <asp:Label runat="server" ID="lblPassword" Text="●●●●●●●●●"></asp:Label> 
         </ItemTemplate> 
         <EditItemTemplate> 
          <asp:TextBox runat="server" ID="txtPassword" TextMode="Password" Text='<%# Bind("Password") %>'> 
          </asp:TextBox> 
         </EditItemTemplate> 
         </asp:TemplateField> 
         <asp:TemplateField HeaderText="Birthdate" SortExpression="Birthdate"> 
         <EditItemTemplate> 
          <asp:TextBox runat="server" ID="Birthdate" Text='<%# Bind("Birthdate") %>' ></asp:TextBox> 
         </EditItemTemplate> 
         <ItemTemplate> 
          <asp:Label runat="Server" ID="lblBirthdate" Text='<%# Bind("Birthdate") %>'></asp:Label> 
         </ItemTemplate> 
         </asp:TemplateField> 
         <asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" /> 
         <asp:BoundField DataField="Apt" HeaderText="Apt" SortExpression="Apt" /> 
         <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" /> 
         <asp:TemplateField HeaderText="Province"> 
         <ItemTemplate> 
          <asp:Label runat="server" ID="lblProvince" Text='<%# Eval("ProvName") %>'></asp:Label> 
         </ItemTemplate> 
         <EditItemTemplate> 
          <asp:DropDownList runat="server" ID="ddlProvince" DataValueField="ProvinceID" DataSourceID="SqlDataSource2" DataTextField="Province"></asp:DropDownList> 
         </EditItemTemplate> 
         </asp:TemplateField> 
         <asp:BoundField DataField="PostalCode" HeaderText="PostalCode" SortExpression="PostalCode" /> 
         <asp:BoundField DataField="PhoneNum" HeaderText="PhoneNum" SortExpression="PhoneNum" /> 
         <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" /> 
         <asp:CheckBoxField DataField="ynAdminUser" HeaderText="ynAdminUser" SortExpression="ynAdminUser" /> 
        </Fields> 
      </asp:DetailsView> 
      <asp:SqlDataSource ID="SqlDataSource2" 
        runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        SelectCommand="SELECT ProvinceID, Province FROM tblProvince"></asp:SqlDataSource> 
        <asp:SqlDataSource ID="SqlDataSource1" 
        runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        SelectCommand="SELECT a.UserID, a.FirstName, a.LastName, a.UserName, a.Password, a.Birthdate, a.Address, a.Apt, a.City, a.Province, b.Province as 'ProvName', 
            a.PostalCode, a.PhoneNum, a.Email, a.ynAdminUser 
            FROM tblUser a 
            INNER JOIN tblProvince B ON A.Province = B.ProvinceID 
            WHERE (a.UserID = @UserID) AND (a.ynDelete = 0)" 
        InsertCommand="INSERT INTO tblUser(FirstName, LastName, UserName, Password, Birthdate, Address, Apt, City, Province, PostalCode, Email, PhoneNum, ynAdminUser, ynDelete) VALUES (@FirstName, @LastName, @UserName, @Password, @Birthdate, @Address, @Apt, @City, @Province, @PostalCode, @Email, @PhoneNum, @ynAdminUser, 0)" 
        UpdateCommand="UPDATE tblUser SET FirstName = @FirstName, LastName = @LastName, Birthdate = @Birthdate, Address = @Address, Apt = @Apt, City = @City, Province = @Province, PostalCode = @PostalCode, PhoneNum = @PhoneNum, Email = @Email, ynAdminUser = @ynAdminUser WHERE (UserID = @UserID)"> 
        <SelectParameters> 
         <asp:ControlParameter ControlID="gvUsers" Name="UserID" PropertyName="SelectedValue" Type="Int32" /> 
        </SelectParameters> 
        <UpdateParameters> 
         <asp:Parameter Name="FirstName" Type="String" /> 
         <asp:Parameter Name="LastName" Type="String" /> 
         <asp:Parameter DbType="DateTime" Name="BirthDate" /> 
         <asp:Parameter Name="Address" Type="String" /> 
         <asp:Parameter Name="Apt" Type="String" /> 
         <asp:Parameter Name="City" Type="String" /> 
         <asp:ControlParameter Name="Province" ControlID="ddlProvince" PropertyName="SelectedValue" Type="Int32" /> 
         <asp:Parameter Name="PhoneNum" Type="String" /> 
         <asp:Parameter Name="Email" Type="String" /> 
         <asp:Parameter Name="ynAdminUser" Type="Boolean" /> 
         <asp:Parameter Name="UserID" Type="Int32" /> 
        </UpdateParameters> 
        <InsertParameters> 
         <asp:Parameter Name="FirstName" Type="String" /> 
         <asp:Parameter Name="LastName" Type="String" /> 
         <asp:Parameter Name="UserName" Type="String" /> 
         <asp:Parameter Name="Password" Type="String" /> 
         <asp:Parameter DbType="DateTime" Name="Birthdate" /> 
         <asp:Parameter Name="Address" Type="String" /> 
         <asp:Parameter Name="Apt" Type="String" /> 
         <asp:Parameter Name="City" Type="String" /> 
         <asp:Parameter Name="Province" Type="Int32" /> 
         <asp:Parameter Name="PostalCode" Type="String" /> 
         <asp:Parameter Name="Email" Type="String" /> 
         <asp:Parameter Name="PhoneNum" Type="String" /> 
         <asp:Parameter Name="ynAdminUser" Type="Boolean" /> 
        </InsertParameters> 
       </asp:SqlDataSource> 
+0

你說'我的頁面崩潰',你可以提供一些細節嗎?有拋出異常嗎? – Phaedrus 2009-11-21 15:16:43

+0

它拋出一個異常,因爲它試圖通過NULL作爲省參數......這表明ddlProvince.SelectedValue沒有按照我的意圖傳遞。 簡單地說,我想通過gridview更新 – Mark 2009-11-21 16:21:53

回答

2

你不需要控制結合:

<asp:ControlParameter Name="Province" ControlID="ddlProvince" PropertyName="SelectedValue" Type="Int32" /> 

相反,像你的其他人一樣使用調整參數,並將以下綁定添加到t的下拉列表中他編輯gridview的模式:

SelectedValue=<%#Bind("Province")%> 
+1

省的更新參數的下拉列表selectedvalue下拉控件中沒有SelecedValue選項...當我嘗試這個(沒有添加SelectedValue),它墜毀試圖傳遞NULL給參數省 – Mark 2009-11-21 14:53:20

+0

哇,我的錯!雖然它沒有出現在intellisense彈出窗口中,但我嘗試了它並且工作...非常感謝 – Mark 2009-11-21 16:35:53

+0

實際選擇了一個省份嗎?如果不是,請嘗試向您的參數添加ConvertEmptyStringToNull =「false」。 SelectedValue是兩種方式綁定下拉列表中數據的方式。 – ScottE 2009-11-21 17:40:50

0

我意識到這是一個非常古老的問題,我希望你找到你的答案。

看你的標記,不過,我想說你的連接字符串從未填寫:

runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 

我不是最強的,當涉及到ASP.NET數據綁定的標記代碼,但我敢肯定你的連接字符串必須指向一個實際字符串名稱在您的的web.config文件的ConnectionStrings部分:

<connectionStrings> 
    <clear /> 
    <add name="Mark1" connectionString="Data Source=localhost;Initial Catalog=Table1;Integrated Security=True;" providerName="System.Data.SqlClient" /> 
</connectionStrings> 

使用任何你一個但是,實際連接字符串是。

runat="server" ConnectionString="<%$ ConnectionStrings:Mark1 %>" 

再說一遍,我是新來的:現在

,爲您的標記應該被寫入。我只是在試圖尋找解決自己問題的方法時才發現這一點。

如果這是錯誤的(可能),有人讓我知道我做錯了什麼,我會刪除它。