English 中文(简体)
passing dropdown value to update parameter for ASP.Net Gridview
原标题:

I have a gridview that, when in edit mode, I use a dropdown to load all provinces, however, when I change the province and click update, my page crashes...I m not sending the province parameter to the gridview s updateparameters properly...here s my code, someone please help..

            <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>
问题回答

You don t need the control binding:

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

Instead, use a regualar parameter like your others and add the following binding to your drop-down list for the edit mode of the gridview:

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

I realize this is a really old question, and I hope you found your answer.

Looking at your markup, though, I want to say your connection strings were never filled out:

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

I am not the strongest when it comes to ASP.NET databinding in the markup code, but I am pretty sure your connection string needs to point to an actual string name in your web.Config file s ConnectionStrings section:

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

Use whatever your actual connection string is, though.

Now, your markup should be written as:

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

Again, I m new to this. I found this only while trying to search for ways to solve my own problems.

If this is wrong (possible), someone just let me know what I did wrong and I ll delete it.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签