简体   繁体   中英

Trying to make parameters for asp.net SqlDataSource and update via gridview

I tried to follow a msdn but apparently I am getting it wrong, or at least missing some key piece of information.

The page is simple, I have a grid view tied to the SqldataSource, and in the update command I have created parameters for the fields in the grid view plus one more for telling my update command which rows to update.

my main question is why its telling me gridview does not contain a definition for 'SelectParameters' (see comment in code). The code there came directly from msdn.

I have some kinks to work out, but first i have to figure out how to pass in the parameters.

thank you

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="RegisterVisitorPortal.aspx.cs" Inherits="resident_RegisterVisitorPortal" %>

<script runat="server">


    //void EmployeesGridView_OnSelectedIndexChanged(Object sender, EventArgs e)
    //{
    //    //gridview does not contain a definition for 'SelectParameters'
    //    GridView1.SelectParameters["VisitorName"].DefaultValue = GridView1.SelectedValue.ToString();
    //    GridView1.SelectParameters["ResidentName"].DefaultValue = Request.LogonUserIdentity.Name;
    //    GridView1.DataBind();
    //}

</script>    

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

    <%--data source object the grid view is tied to--%>

    <asp:SqlDataSource 
        ID="SqlDataSource1" 
        runat="server" 
        ConnectionString="<%$ ConnectionStrings:Warren_SEINDATASYSTEMSConnectionString %>" 
        SelectCommand="SELECT [VisitorCode], [VisitorName] FROM [RegisteredVisitors] WHERE [ResidentName] = @ResidentName"
        OnSelecting="SqlDataSource1_Selecting"
        UpdateCommand="UPDATE [dbo].[RegisteredVisitors] SET [VisitorCode] = '3222' ,[VisitorName] = @VisitorName WHERE [ResidentName] = @ResidentName" 
        >
        <SelectParameters>
            <asp:Parameter Name="VisitorName" Type="String" DefaultValue="Visitor" />
            <asp:Parameter Name="ResidentName" Type="String" DefaultValue="ResidentName" />
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="VisitorName" Type="String" DefaultValue="Visitor" />
            <asp:Parameter Name="ResidentName" Type="String" DefaultValue="ResidentName" />
        </UpdateParameters>
    </asp:SqlDataSource>

    <%--grid view showing and allowing edit of data--%>

    <asp:GridView ID="GridView1" runat="server" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" CellPadding="4" ForeColor="#333333">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <asp:BoundField DataField="VisitorCode" HeaderText="VisitorCode" SortExpression="VisitorCode" ReadOnly="true"/>
            <asp:BoundField DataField="VisitorName" HeaderText="VisitorName" SortExpression="VisitorName" />
        </Columns>
        <EditRowStyle BackColor="#999999" BorderColor="Black" BorderWidth="1px" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
    </asp:GridView>



</asp:Content>

定义SqlDataSource ,在select语句中未使用任何参数,因此不需要SqlDataSource SelectParameters部分,但需要UpdateParameters部分

  <selectparameters>
              <asp:controlparameter name="VisitorName" controlid="hiddenfiled1" propertyname="value"/>
          </selectparameters>
  <selectparameters>
              <asp:controlparameter name="ResidentName" controlid="hiddenfiled2" propertyname="value"/>
          </selectparameters>

Store value in Hidden File in on editing Event . use hidden filed value as parameter in SqlDataSource to make it working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM