如何从控制事件中获取值到Page_Preinit在Asp.Net中的阶段? [英] How Do I Get The Values From Control Events To Page_Preinit Phase In Asp.Net?

查看:137
本文介绍了如何从控制事件中获取值到Page_Preinit在Asp.Net中的阶段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个aspx页面,我正在创建几个动态控件。



我也在Page_PreInit阶段重新创建所有这些动态控件,因为我需要渲染当事件发生或页面被发回服务器时它们。



但问题是,我想从控件DropDownList中检索所选值DropDownlist Change事件发生时。由于我在PreInit阶段重新创建所有动态控件,我无法在该阶段获得Dropdownlist选择值。?



请指教。



提前致谢。



代码例。



I have a aspx page where i am creating several dynamic controls.

I am also Re-creating all these dynamic controls in Page_PreInit phase since i need to render them when an event happens or when the page is posted back to server.

But Now the problem is, i want to retrieve the selected value from a control "DropDownList" when the DropDownlist Change event occurs. And Since i am Re-creating all Dynamic Controls in PreInit phase, i cannot get the Dropdownlist selected value in that phase.?

Please advise.

Thanks in advance.

Code Ex.

protected void Page_PreInit(object sender, EventArgs e)
    {
        //recreating all dyn controls on every postback in order to access them..
        if (IsPostBack)
        {
            Custom_Org_Chart.Draw_Entire_Chart(this.Page, form1,this.GetType(),Convert.ToInt32(DropDownList1.SelectedItem.Value));
        }
    }



此处将根据DropDownlist选择的项目值呈现动态控件

但是(DropDownList1.SelectedItem.Value)将抛出NullRefrenceException。



注意:

1.我试图得到OverRiding Pre-Render事件的值,但它不起作用..

2.还尝试重新创建DropDownList Change事件的所有动态控件,它抛出了重复ID的发现错误,因为控件正在创建两次..


Here the Dynamic controls will be rendered, based on the DropDownlist Selected Item Value
but (DropDownList1.SelectedItem.Value) will throw NullRefrenceException.

Note:
1. I tried to get the value by OverRiding Pre-Render Event, but it didn't work..
2. Also tried re-creating all dynamic controls on DropDownList Change event, it threw duplicate ID's found error, since controls were creating twice..

推荐答案

您需要使用Page OnInit事件来创建动态控件。



< a href =http://msdn.microsoft.com/en-us/library/cd6at422(v=vs.110).aspx> http://msdn.microsoft.com/en-us/library/cd6at422( v = vs.110).aspx [ ^ ]
You need to use the Page OnInit event to create dynamic controls.

http://msdn.microsoft.com/en-us/library/cd6at422(v=vs.110).aspx[^]


我认为你真正得到的是if(isPostBack)。看起来很奇怪,你想要在回发时重新创建动态控件,但由于网页是无状态的,它不知道你的控件是什么了。我自己做了一点运动来证明这一点。在aList_SelectedIndexChanged事件上放置一个断点,它将在回发时使用正确的值访问它。





I think what's really getting you is the if(isPostBack). It seems weird, that you'd want to recreate your dynamic controls on postback, but since a web page is stateless, it doesn't know what your control is anymore. I did a little exercise myself to prove it. Put a breakpoint on the aList_SelectedIndexChanged event and it will visit it on postback with the right value.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
    <title></title>
</head>
<body>
    <form id="form1"  runat="server">
    <asp:Panel ID="pnlMain" runat="server" Style="width: 100%;">
    </asp:Panel>
    </form>
</body>
</html>







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

public partial class _Default : System.Web.UI.Page
{
    protected override void OnPreInit(EventArgs e)
    {
        base.OnPreInit(e);
        CreateDropDown();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    private void CreateDropDown()
    {
        DropDownList aList = new DropDownList();
        for (int i = 0; i < 5; i++)
        {
            aList.Items.Add(new ListItem(i.ToString(), i.ToString()));
        }
        aList.SelectedIndexChanged += new EventHandler(aList_SelectedIndexChanged);
        aList.AutoPostBack = true;
        pnlMain.Controls.Add(aList);
    }

    private void aList_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList aList = (DropDownList)sender;
        int i = -1;
        if (Int32.TryParse(aList.SelectedValue, out i))
        { 
            //good to go
        }
    }
}


这篇关于如何从控制事件中获取值到Page_Preinit在Asp.Net中的阶段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆