aspx 文件
       <div class="c_MainLeft">
<ul style="text-align:left;">
    <li style="list-style-type: none;height:130px;">
        <img src="images/038.png" alt="说明"/></li><li style="list-style-type: none;height:40px;">
    共有<asp:Label ID="lbl_Count" runat="server" Style="color: red"></asp:Label>条发放记录</li><li style="list-style-type: none;height:40px;"><asp:TextBox ID="Yuan_Name" runat="server"
            CssClass="c_input" style="width: 80px"></asp:TextBox>
        <input id="butYuan" class="c_button" name="butYuan" style="width: 30px" type="button"
            value="..." onclick="return butYuan_onclick()" />
        <asp:HiddenField ID="Yuan_ID" runat="server" />
    </li>
<li style="list-style-type: none;height:40px;padding:0px;">
                <asp:Button ID="Button1" runat="server" CssClass="c_button" Text="按员工查询" OnClick="butYuan_Click" />
                <asp:Button ID="butAll" runat="server" CssClass="c_button" Text="查询全部" OnClick="butAll_Click" /></li></ul>
        </div>
    <div class = "c_MainRight" style="text-align:left " >
       <fieldset class="c_edit">
<legend>员工工资明细信息</legend>

          <asp:Button ID="butSave" runat="server" CssClass="c_button" Text="保存" OnClick="butSave_Click" />
                &nbsp;&nbsp;&nbsp;&nbsp;
          <asp:Button ID="butReturn" runat="server" CssClass="c_button" Text="返回" OnClick="butReturn_Click" /> <ul>
<asp:Table id="Table1" 
             style="Z-INDEX: 101; POSITION: absolute; " runat="server" ></asp:Table>

        </ul>
<ul>
            <asp:HiddenField ID="Hid_Command" runat="server" />
            <asp:HiddenField ID="Hid_RowIndex" runat="server" />
        </ul>
         </fieldset>   
   </div>
        
</asp:Content>
cs文件using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class YuanPayrollsEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            int intRowsIndex = 0;
            if (Request.QueryString["RowIndex"] != null)
            {
                intRowsIndex = int.Parse(Request.QueryString["RowIndex"].ToString());
                Hid_RowIndex.Value = intRowsIndex.ToString();
            }
            String strCommand = "";
            if (Request.QueryString["Command"] != null)
            {
                strCommand = Request.QueryString["Command"].ToString();
                Hid_Command.Value = strCommand;
            } 
            if (strCommand == "Edit")
        {
               //权限查询
             publicFunction pFunction = new publicFunction();
 
            PayrollEdit("add", intRowsIndex);
            //PayrollEdit("minus", intRowsIndex);
           this.FindControl("this.Tlbtop").Focus();
} }
    }
    protected void PayrollEdit(string str,int intRowsIndex)
    {
        if (str == "add")
        {
            //获得数据表
            DataTable T_Yuan_payrolls = (DataTable)Session["T_Yuan_payrolls"];
            String Edit_date = T_Yuan_payrolls.Rows[intRowsIndex]["Date"].ToString();
            String Edit_Name = T_Yuan_payrolls.Rows[intRowsIndex]["Name"].ToString();            //建立数据源
            Yuan_PayrollsEdit yuan_payrollsEdit = new Yuan_PayrollsEdit();
            DataSet ds_Yuan_payrollsEdit = yuan_payrollsEdit.GetDataSet(Edit_date, Edit_Name);            string aa = ds_Yuan_payrollsEdit.Tables["T_Yuan_payrollsEdit_add"].Rows.Count.ToString();
            TableRow tr = new TableRow();
            TableCell tc;
            TableCell cell;
            TextBox tc1;
            int i = 0;
            decimal sum_je = 0.00m;
            for (i = 0; i < int.Parse(aa); i++)
            {
                if (i == 0)
                {
                    tr = new TableRow();
                    //tr = new TableRow();
                    tc = new TableCell();
                    tc.ID = "Labeltop";
                    cell = new TableCell();
                    Label tcc = new Label();
                    Label tcc1 = new Label();
                    tcc.ID = "Tlbtop";
                    tc.Text = Edit_date;
                    tcc.Text = Edit_Name.Remove(0, 6);
                    tcc1.Text = aa;
                    tcc1.Visible = false;
                    tc.CssClass = "c_label";
                    tcc.CssClass = "c_label";

                    tr.Cells.Add(tc);
                    cell.Controls.Add(tcc);
                    cell.Controls.Add(tcc1);
                    tr.Cells.Add(cell);
                    Table1.Rows.Add(tr);                }
                if (i % 3 == 0)
                    tr = new TableRow();
                tc = new TableCell();
                tc.ID = "Label" + ds_Yuan_payrollsEdit.Tables["T_Yuan_payrollsEdit_add"].Rows[i][0].ToString();
                cell = new TableCell();
                tc1 = new TextBox();
                tc1.ID = "textid" + i.ToString();
                tc1.Columns = 8;
                tc1.CssClass = "c_textbox";
                tc.CssClass = "c_label";
                tc1.Text = ds_Yuan_payrollsEdit.Tables["T_Yuan_payrollsEdit_add"].Rows[i][2].ToString();
                sum_je += decimal.Parse(ds_Yuan_payrollsEdit.Tables["T_Yuan_payrollsEdit_add"].Rows[i][2].ToString());
                tc.Text = ds_Yuan_payrollsEdit.Tables["T_Yuan_payrollsEdit_add"].Rows[i][1].ToString() + ":    ";
                //tc1.Attributes.Add("onkeydown", "if(event.keyCode==13)event.keyCode=9");
                tc1.Attributes.Add("onkeydown", "return CheckInput(this.value)");
                tc1.Attributes.Add("LOSTFOCUS", "return CheckOutput(tc1.value)");
                tr.Cells.Add(tc);
                cell.Controls.Add(tc1);
                tr.Cells.Add(cell);
                if (i % 3 == 0)
                {
                    Table1.Rows.Add(tr);
                }
            }
            if (i % 3 != 0)
                Table1.Rows.Add(tr);
            tr = new TableRow();
            //tr = new TableRow();
            tc = new TableCell();
            tc.ID = "Labelsum";
            cell = new TableCell();
            Label sum_tcc = new Label();
            Label sum_tcc1 = new Label();
            sum_tcc.ID = "Tlbsum";
            tc.Text = "小  计:";
            sum_tcc.Width = 95;
            sum_tcc.Text = sum_je.ToString();
            sum_tcc1.Text = aa;
            //sum_tcc1.Visible = false;
            tc.CssClass = "c_label_sum";
            sum_tcc.CssClass = "c_label_sum";
            tr.Cells.Add(tc);
            cell.Controls.Add(sum_tcc);
            cell.Controls.Add(sum_tcc1);
            tr.Cells.Add(cell);
            Table1.Rows.Add(tr);
        }
  
    }
 }
动态生成控件 texbox 并将id 设为 tlbtop                    tc.ID = "Labeltop";
                    cell = new TableCell();
                    Label tcc = new Label();
                    Label tcc1 = new Label();
                    tcc.ID = "Tlbtop";
                    tc.Text = Edit_date;但是在引用“Tlbtop”            this.FindControl("this.Tlbtop").Focus();
系统报错:未将对象引用设置到对象的实例。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
行 101:            PayrollEdit("add", intRowsIndex);
行 102:            PayrollEdit("minus", intRowsIndex);
行 103:            this.FindControl("this.Tlbtop").Focus();
行 104:
行 105: }
 
问下 动态生成的控件 该如何引用、读取值,及设置焦点。 谢谢 

解决方案 »

  1.   

    呵呵,过来看看哦,focus();顶顶
      

  2.   

    focus() ...
     偶当有回答了呢
      

  3.   

    Label lbl=this.FindControl("Tlbtop") as Label;
    private void AddTextBoxs() 
            { 
                TableRow tr=new TableRow(); 
                TableCell tc1=new TableCell(); 
                TextBox t=new TextBox(); 
                t.ID="tb"+Table1.Rows.Count; 
                tc1.Controls.Add(t);        
                tr.Cells.Add(tc1); 
                Table1.Rows.Add(tr); 
            } 
     
    for(int i=0;i<Table1.Rows.Count;i++) 
                { 
                    Response.Write(((TextBox)Table1.Rows[i].FindControl("tb"+i)).Text+"<br>"); 
                } 
      

  4.   

             decimal tlb_sum= 0.0m;
                tlb_sum = decimal.Parse(((Label)Table1.FindControl("Tlbsum")).Text);
    这样解决 谢谢