Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ASP.NET用户控件输入参数_Asp.net_User Controls - Fatal编程技术网

ASP.NET用户控件输入参数

ASP.NET用户控件输入参数,asp.net,user-controls,Asp.net,User Controls,我编写的第一个UserControl如下所示: public partial class DefectMap : System.Web.UI.UserControl { const string imageProviderUrl = "~/DefectMapImageProvider.ashx?"; public long MapID { get; set; } public int Rows { get; set; } public int Cols { ge

我编写的第一个UserControl如下所示:

public partial class DefectMap : System.Web.UI.UserControl
{
    const string imageProviderUrl = "~/DefectMapImageProvider.ashx?";

    public long MapID { get; set; }
    public int Rows { get; set; }
    public int Cols { get; set; }
    public int? Width { get; set; }
    public int? Height { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        ComposeImageUrl();
        GenerateTableStructure();
    }

    void ComposeImageUrl()
    {
        StringBuilder builder = new StringBuilder(imageProviderUrl);
        builder.AppendFormat("DefectMapId={0}", MapID);

        // set up width & height
        if (Width != null && Width > 0)
        {
            builder.AppendFormat("&Width={0}", Width);
            MapImage.Width = (Unit)Width;
        }

        if (Height != null && Height > 0)
        {
            builder.AppendFormat("&Height={0}", Height);
            MapImage.Height = (Unit)Height;
        }

        MapImage.ImageUrl = builder.ToString();
    }

    void GenerateTableStructure()
    {
        if (Rows > 0 && Cols > 0)
        {
            TableHelper.CreateStructure(MapTable, Rows, Cols);
        }
    }
}
如果我在某些页面上添加此控件并在标记中设置值

<uc:DefectMap ID="DefectMap" runat="server" Height="80" MapID="1" />

是的,页面加载太早-值不可用。我的第一选择是重写DataBind方法并将代码放在那里,或者重写OnPreRender方法并将其放在那里。

我找到了与Page\u Load:Page\u PreRender类似的方法。不管它在哪里,它都能工作。非常感谢。
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridView grid = (GridView)sender;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DefectMap defectMap = (DefectMap)e.Row.FindControl("DefectMap");
            DEFECTMAP data = (DEFECTMAP)e.Row.DataItem;

            defectMap.MapID = data.ID_DEFECTMAP;
            defectMap.Rows = data.ROWS;
            defectMap.Cols = data.COLS;
        }
    }