PDA

View Full Version : حرفه ای: عدم دسترسی به کنترهایی که run time ایجاد می شوند



goodarzi121
چهارشنبه 22 اردیبهشت 1389, 08:38 صبح
سلام به همه:
برنامه ای دارم که با master page نوشتم. یک کنترل PlaceHolder دارم که یک جدول با نام tb در run time داخل PlaceHolder می سازم. می تونم به PlaceHolder دسترسی داشته باشم اما به tb نه!
لظفا کمک کنید.
سورس برنامه هم اینه :

<%@ Page Title="انتخاب ستون" Language="C#‎‎‎" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="SelectColumn.aspx.cs" Inherits="Test" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<table style="width: 100%;" dir="rtl" id="tb_1">
<tr>
<td>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
&nbsp;<asp:UpdateProgress ID="UpdateProgress1" runat="server">
<ProgressTemplate>
&nbsp;&nbsp;
<asp:Image ID="Image1" runat="server" ImageUrl="~/Images/loading.gif" />
&nbsp; لطفا چند لحظه صبر کنید ...
</ProgressTemplate>
</asp:UpdateProgress>
</td>
<td>
&nbsp;</td>
<td>
&nbsp;</td>
</tr>
<tr>
<td>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:PlaceHolder ID="ph1" runat="server"></asp:PlaceHolder>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="ادامه"
Width="47px" />
</ContentTemplate>
</asp:UpdatePanel>
</td>
<td>
&nbsp;</td>
<td>
&nbsp;</td>
</tr>
<tr>
<td>
&nbsp;</td>
<td>
&nbsp;</td>
<td>
&nbsp;</td>
</tr>
</table>
</asp:Content>



و برنامه ی C#‎‎:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dbName = Common.QueryString("db");
tbNames = Common.Session_Script;
DataTable dt = new DataTable();
Common.Session_Tables = new string[] { "" };
Common.Session_Cols = "";

if (dbName != "")
{
dt = GetColumns(dbName, tbNames );
Common.Session_DataSource = dt;
}

HtmlTable tb = new HtmlTable();
tb.Align = "center";
tb.ID = "tb";
uid= tb.UniqueID;
tb.Width = "100%";
tb.Border = 2;
HtmlTableRow trh = new HtmlTableRow();
trh.BgColor = "#FFCCCC";
HtmlTableCell tch0 = new HtmlTableCell();
tch0.Align = "center";
tch0.Width = "40px";
Label lbh0 = new Label();
lbh0.Text = "انتخاب";
lbh0.ID = "lbh0";
tch0.Controls.Add(lbh0);
HtmlTableCell tch1 = new HtmlTableCell();
tch1.Align = "center";
tch0.Width = "60px";
Label lbh1 = new Label();
lbh1.Text = "نام ستون";
lbh1.ID = "lbh1";
tch1.Controls.Add(lbh1);
HtmlTableCell tch2 = new HtmlTableCell();
tch2.Align = "center";
tch0.Width = "100px";
Label lbh2 = new Label();
lbh2.Text = "نام ستون در گزارش";
lbh2.ID = "lbh2";
tch2.Controls.Add(lbh2);
trh.Controls.Add(tch0);
trh.Controls.Add(tch1);
trh.Controls.Add(tch2);
tb.Rows.Add(trh);

int n = 0;
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
HtmlTableRow tr = new HtmlTableRow();
if (i % 2 == 1)
{
tr.BgColor = "#A8C1FD";
}
HtmlTableCell tc1 = new HtmlTableCell();
tc1.Align = "center";
CheckBox chbx = new CheckBox();
chbx.Width = 180;
chbx.ID = "chbx" + i + n;
EventHandler(cmbDataSource_SelectedIndexChanged);
tc1.Controls.Add(chbx);

HtmlTableCell tc2 = new HtmlTableCell();
tc2.Align = "center";
Label lb = new Label();
lb.EnableViewState = true;
lb.Text = dr["col_name"].ToString();
lb.ID = "lb" + i + n;
tc2.Controls.Add(lb);

HtmlTableCell tc3 = new HtmlTableCell();
tc3.Align = "center";
TextBox txt = new TextBox();
txt.Width = 180;
txt.EnableViewState = true;
txt.Text = dr["col_name"].ToString();
txt.ID = "txt" + i + n;

if (i % 2 == 1)
{ txt.BackColor = Color.FromName("#A8C1FD"); }
EventHandler(cmbColumn_SelectedIndexChanged);
tc3.Controls.Add(txt);

tr.Cells.Add(tc1);
tr.Cells.Add(tc2);
tr.Cells.Add(tc3);

tb.Rows.Add(tr);
}
ph1.Controls.Add(tb);

}
}

protected void Button1_Click(object sender, EventArgs e)
{
string[] Tables = Common.Session_Tables;
Control c = Page.FindControl("ctl00$ContentPlaceHolder1$ph1");
string cols = "";
if (c != null)
{
HtmlTable tb = c as HtmlTable;

using (ReportGenerator rg = new ReportGenerator())
{
for (int i = 0; i < tb.Rows.Count; i++)
{
CheckBox chbx = tb.Rows[i].Cells[0].Controls[0] as CheckBox;
if (chbx != null && chbx.Checked)
{
Label lb = tb.Rows[i].Cells[1].Controls[0] as Label;
TextBox txt = tb.Rows[i].Cells[2].Controls[0] as TextBox;
cols += lb.Text + " as [" + txt.Text + "],";
rg.Add(lb.Text, txt.Text, false, false, -1, -1);
}
}
Common.Session_Fields = rg.GetReportFields;
}
Common.Session_DataSourceCommand = "use " + dbName + " select " + cols + " from " + Tables[Tables.Length - 1];// +" where ";
Response.Redirect("Preview.aspx");
}
}

متشکرم