无法从GridView访问存储在隐藏字段中的值

时间:2015-10-25 13:48:55

标签: c# asp.net gridview hiddenfield

我有一个销售订单ID,我需要作为我订购的产品的GridView的SelectParameter,所以我将它存储在一个隐藏的字段中(在GridView之外)。在同一页面上,我正在向此订单添加产品,然后重新绑定GridView以包含该产品。每次我的参数值都为null,所以我不认为它从隐藏字段获取值。我想过将它存储在ViewState中但不认为你可以从GridView SelectParameter中检索ViewState值,或者你可以吗?无论如何,如果有人能告诉我为什么在GridView之外使用隐藏字段值将无法工作(它仍然在表单中),那将是fab!另外,我想要一个使用ViewState的选项,所以需要知道我的SelectParameter代码是什么。代码如下:

<form id="form1" runat="server">
    <asp:HiddenField ID="hSOId" runat="server" />
<div>
<div class="row">
        <div class="col-md-12">
        <strong>Products:</strong>
        <asp:GridView ID="gvOptions" runat="server" AutoGenerateColumns="False" CellPadding="10" CellSpacing="10" DataSourceID="SqlDataSource1" 
        ForeColor="#333333" GridLines="None" EmptyDataText="No Products Added" >
        <AlternatingRowStyle BackColor="White" />
        <Columns>
        <asp:BoundField DataField="Internal_Name" HeaderText="Product" SortExpression="Internal_Name" />
        <asp:BoundField DataField="Qty" HeaderText="Qty" SortExpression="Qty" />
        <asp:BoundField DataField="Total_Kilo_Weight" HeaderText="Weight" SortExpression="Total_Kilo_Weight" />
        <asp:BoundField DataField="Sales_Price" HeaderText="Price" SortExpression="Sales_Price" />
        <asp:BoundField DataField="ProfitMargin" HeaderText="Profit" SortExpression="ProfitMargin" />               
        <asp:BoundField DataField="Co_Depot" HeaderText="Depot" SortExpression="Co_Depot" />
        <asp:BoundField DataField="Pack_Date" HeaderText="Pack Date" DataFormatString="{0:d}" SortExpression="Pack_Date" />
        <asp:BoundField DataField="Delivery_Date" HeaderText="Delivery Date" DataFormatString="{0:d}" SortExpression="Delivery_Date" />
        </Columns>
        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F5F7FB" />
        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
        <SortedDescendingCellStyle BackColor="#E9EBEF" />
        <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyConn %>" 
        SelectCommand="spSalesOrderProductsRead" SelectCommandType="StoredProcedure" OnSelecting="gvOptions_Selecting">
        <SelectParameters>
            <asp:FormParameter FormField="hSOId" Name="SalesOrderID"     Type="Int32" />
        </SelectParameters>
        </asp:SqlDataSource>
        </div><!-- end of column -->
    </div><!-- end of row -->
</div>
</form> 

2 个答案:

答案 0 :(得分:0)

我想你不能在HTML代码中做到这一点,而只是背后的代码(C#或VB) - 或者至少在外面。

了解如何获取HiddenField信息:

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hiddenfield.value(v=vs.110).aspx

答案 1 :(得分:0)

回到老式的方式,它的工作原理!我将销售订单ID保存在View State对象中,然后在我想要重新绑定时检索它并且它完美地工作。仍然想知道是否有办法只是调用gvOptions.Bind();并以某种方式让SelectParameter检索视图状态值 - 我敢肯定必须有一种方法。无论如何,现在感到高兴,希望这可以帮助别人:

  protected void rebindGVProducts()
  {
    string strError = "";
    int soID = 0;

    if(ViewState["SOID"] != null)
        soID = Convert.ToInt32(ViewState["SOID"]);
    //hSOId.Value = soID;

    try
    {
        //Get all Products in this sales order
        ConnectionStringSettings SHConn = ConfigurationManager.ConnectionStrings["Conn"];
        SqlConnection objConn = new SqlConnection(SHConn.ConnectionString);
        //SqlDataReader objRdr;
        SqlCommand objCmd;

        objConn.Open();
        objCmd = new SqlCommand("spSalesOrderProductsRead", objConn);
        objCmd.CommandType = CommandType.StoredProcedure;
        objCmd.Parameters.AddWithValue("@SalesOrderID", soID);
        SqlDataAdapter adapter = new SqlDataAdapter(objCmd);
        DataSet ds = new DataSet();

        adapter.Fill(ds, "Products");

        gvProducts.DataSource = ds;
        gvProducts.DataSourceID = String.Empty;
        gvProducts.DataBind();

        //objRdr.Close();
        objConn.Close();
    }
    catch (SqlException objSqlDbException)
    {
        foreach (SqlError objError in objSqlDbException.Errors)
        {
            //Response.Write("Error is: "+objError);
            strError += objError;
        }
        //add errors to error log file ErrorLogFile.txt
        reportError(strError);

        Response.Write("Sorry - we are currenly experiencing problems with our database, please try again later.");
    }
}