如何在回发时将控制clientID添加到Request.Form集合

时间:2011-12-02 20:48:00

标签: c# javascript asp.net

很快就需要帮助......

我有一个Gridview,其中的列主要是“Itemtemplates”,除了在渲染时为其中的控件提供的客户端ID之外,没有任何特定的ID。

我目前正在对现有页面进行更改,并且在gridview视图集中的控件的很少客户端ID出现在Request.Form集合中,而且很少。  我需要将客户端ID添加到Request.Form集合,并在发回服务器时从那里访问它的值。

如何/何时将控件添加到Request.Form集合中?我很困惑。 任何帮助将受到高度赞赏。

由于

将动态列添加到网格的代码:

else if (Mode != ProductGridMode.Search)
    {
        grid.Columns.Add(CreateTemplateField(
            path + "ProductDetailHeader.ascx",
            path + "ProductDetailCell.ascx"));

        grid.Columns.Add(CreateTemplateField(
            path + "CustomerPartNumberHeader.ascx",
            path + "CustomerPartNumberCell.ascx", "td-partnumber"));

        grid.Columns.Add(CreateTemplateField(
            path + "OrderQuantityHeader.ascx",
            path + "OrderQuantityCell.ascx", "td-qty"));

        if (Mode == ProductGridMode.OrderHistoryPayments
            || Mode == ProductGridMode.OrderHistoryPaymentsPrintView)
        {
            grid.Columns.Add(CreateTemplateField(
                path + "AvailableToShipHeader.ascx",
                path + "AvailableToShipCell.ascx"));
        }

        grid.Columns.Add(CreateTemplateField(
            path + "PriceHeader.ascx",
            path + "PriceCell.ascx", "td-price"));

        grid.Columns.Add(CreateTemplateField(
            path + "ExtendedPriceHeader.ascx",
            path + "ExtendedPriceCell.ascx", "td-ext-price"));

        if (Mode == ProductGridMode.OrderHistory || Mode == ProductGridMode.OrderHistoryPrintView
            || Mode == ProductGridMode.OrderHistoryPayments || Mode == ProductGridMode.OrderHistoryPaymentsPrintView)
        {
            grid.Columns.Add(CreateTemplateField(
                path + "InvoiceHeader.ascx",
                path + "InvoiceCell.ascx"));

            grid.EmptyDataTemplate = LoadTemplate(path + "EmptyDataTemplate.ascx");
        }
    }
    else
    {

        grid.Columns.Add(CreateTemplateField(
            path + "ImageHeader.ascx",
            path + "ImageCell.ascx"));

        grid.Columns.Add(CreateTemplateField(
            path + "MouserPartNumberHeader.ascx",
            path + "MouserPartNumberCell.ascx"));

        grid.Columns.Add(CreateTemplateField(
            path + "MfrPartNumberHeader.ascx",
            path + "MfrPartNumberCell.ascx"));

        grid.Columns.Add(CreateTemplateField(
            path + "ManufacturerHeader.ascx",
            path + "ManufacturerCell.ascx"));

        grid.Columns.Add(CreateTemplateField(
            path + "DescriptionHeader.ascx",
            path + "DescriptionCell.ascx"));

        grid.Columns.Add(CreateTemplateField(
            path + "DocumentHeader.ascx",
            path + "DocumentCell.ascx"));

        grid.Columns.Add(CreateTemplateField(
            path + "AvailabilityHeader.ascx",
            path + "AvailabilityCell.ascx"));

        grid.Columns.Add(CreateTemplateField(
            path + "PricingHeader.ascx",
            path + "PricingCell.ascx"));

        grid.Columns.Add(CreateTemplateField(
            path + "QuantityHeader.ascx",
            path + "QuantityCell.ascx"));

        grid.Columns.Add(CreateTemplateField(
            path + "OrderInfoHeader.ascx",
            path + "OrderInfoCell.ascx"));

        grid.Columns.Add(CreateTemplateField(
            path + "RoHSHeader.ascx",
            path + "RoHSCell.ascx"));

        // Don't include Select column in count
        Search_TemplateColumnCount = grid.Columns.Count - CellOffset;

        // Due to their nature, the dynamic columns must be created as late as possible which is
        // right before databinding instead of here.
    }

用于其中一个项目模板中使用的用户控件的aspx(我关心的那个)

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="MfrPartNumberCell.ascx.cs"
    Inherits="Controls_ProductGridTemplates_Search_MfgPartNumberCell"        WhitespaceCleaning="Clean" %>
<div style="text-align: left;">
        <%--<asp:Literal ID="litMfrPartNumber" runat="server" Text='<%# Bind("MfrPartNumber") %>' /><br />--%>
        <asp:HyperLink ID="MfrPartNumberLink" runat="server" Text='<%# Bind("MfrPartNumber") %>'
        OnDataBinding="MfrPartNumberLink_DataBinding" /><br />
    <br />
    <asp:HyperLink ID="lnkCrossReference" runat="server" CssClass="highlight" Style="color: #FF6600"
        Target="_blank" OnDataBinding="lnkCrossReference_DataBinding" />
    <div style="width: 100%; text-align: center;">
        <asp:PlaceHolder ID="PHlifecycle" runat="server"></asp:PlaceHolder>
    </div>
</div>

用户控制的代码:

    public partial class Controls_ProductGridTemplates_Search_MfgPartNumberCell : System.Web.UI.UserControl
{
    protected void Page_PreRender(object sender, EventArgs e)
    {
        lnkCrossReference.Visible = (lnkCrossReference.NavigateUrl != string.Empty);
    }

    protected void MfrPartNumberLink_DataBinding(object sender, EventArgs e)
    {
        SearchResultItem searchItem = BusinessHelper.GetDataItem(this) as SearchResultItem;

        MfrPartNumberLink.NavigateUrl = BusinessHelper.BuildProductDetailUrl(searchItem);

        //life cycle
        if (searchItem.LifeCycle != "")
        {
            if (!(searchItem.XXXX== false && searchItem.LifeCycle.ToLower().Trim() == "new at mouser"))
            {
                string lifecycleInfo = Mouser.BOM.ExcelHelper.ProductRules.BuildLifecycleText(searchItem.LifeCycle);
                if (lifecycleInfo != "")
                    PHlifecycle.Controls.Add(new LiteralControl(lifecycleInfo + "<br/><br/>"));
            }
        }

    }

    protected void lnkCrossReference_DataBinding(object sender, EventArgs e)
    {
        SearchResultItem searchItem = BusinessHelper.GetDataItem(this) as SearchResultItem;

        if (!string.IsNullOrEmpty(searchItem.CrossReference))
        {
            StringBuilder crossRefUrlBuilder = new StringBuilder();
            crossRefUrlBuilder.Append("~/Search/CrossPopUp.aspx?xpn=");
            crossRefUrlBuilder.Append(HttpUtility.UrlEncode(searchItem.MfrPartNumber));
            crossRefUrlBuilder.Append("&criteria=");
            crossRefUrlBuilder.Append(HttpUtility.UrlEncode(searchItem.CrossReference));

            lnkCrossReference.NavigateUrl = crossRefUrlBuilder.ToString();
            lnkCrossReference.Attributes.Add("onclick",
                "javascript:window.open('" + ResolveUrl(lnkCrossReference.NavigateUrl)
                + "','CatalogPage', 'height=300,width=400,toolbars=no,menubar=no,location=no');return false;");
            lnkCrossReference.Text = String.Format(Resources.MyCXXXX.lnkCrossReference, searchItem.CrossReference);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

HTML input中的任何HTML form控件都将包含在Request.Form集合中。这些值包含在POST操作的HTTP标头中。

HTTP Headers for Dummies

修改:添加了代码

的Default.aspx

<%@ Page Title="Home Page" Trace="true" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="SO8362448._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> </asp:Content> <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:GridView ID="gridView" runat="server" OnRowDataBound="gridView_rowDataBound" AutoGenerateColumns="false">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Literal runat="server" Text='<%# Eval("Name") %>' />
                    <asp:LinkButton id="hyperLink" runat="server" Text="Click Me" />
                    <asp:HiddenField ID="hiddenField" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView> </asp:Content>

default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SO8362448
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            gridView.DataSource = new List<Person>
            {
                new Person {Name = "Steve", Age = 21},
                new Person {Name = "Cindy", Age = 34}
            };
            gridView.DataBind();
        }

        protected void gridView_rowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lb = e.Row.FindControl("hyperLink") as LinkButton;
                if (lb != null)
                {
                    HiddenField hf = e.Row.FindControl("hiddenField") as HiddenField;
                    if (hf != null)
                    {
                        hf.Value = lb.Text;
                    }
                }
            }
        }
    }
}