Right to appease everyone I have gone away & tried to get my head round the concepts of this! So I do now a have slightly a better grasp of what’s going on, but I still cant get the solution, so I will try to give as much info as possible.
I have a webform (callLogEdit.aspx) from which I am passing a number of textbox & DLL values to another webform (OrderEdit.aspx) which are used to populate equivalent DDL’s & text boxes.
OrderEdit.aspx is used to either display the details of an existing order or allow the user to input the details of a new order.
By calling OrderEdit from CallLogEdit and passing these values I am essentially helping the user create a new order by pre-populating certain fields.
My problem arises because one of the values im passing needs to populate a textbox (txtOrderLine) inside a datalist (dlLine)
This Datalist holds something called order lines,
Normally if the user were to manually create a new order, they would populate the various textboxes, select the various DDL values etc and then to add an order line (i.e a new record in dlLine) they would click on a button which would display an instance of txtOrderLine for editing. Here is the code
Code:
protected void btnLineAdd_Click(object sender, System.EventArgs e)
{
OrderLine OrderLine = new OrderLine();
m_Order.OrderLine.Add(OrderLine);
dlLine.EditItemIndex = m_Order.OrderLine.Count - 1;
Page.DataBind();
}
Now for me to access the textbox I have basically manually forced the btnLineAdd_Click event in the page_load code of order edit.
Code:
//if existing order
if (Request.QueryString["OrderId"] != null)
{
m_Order = new Order(Convert.ToInt32 (Request.QueryString["OrderId"]));
Populate();
}
//if new order
Else
{
m_Order = new Order();
txtAuthor.Text = User.Identity.Name;
txtEntered.Text = System.DateTime.Today.ToShortDateString();
if (Page.PreviousPage != null)
{
//manually trigger click event
btnLineAdd_Click(null, EventArgs.Empty);
ddlBranch.SelectedValue = Convert.ToString(PreviousPage.ConvertToOrderBranchID);
ddlDelivery.SelectedValue = Convert.ToString(PreviousPage.ConvertToOrderBranchID);
}
}
]
And then to actually try & access the textbox I added the ItemCreated event-handling method to OnInit.
Code:
override protected void OnInit(EventArgs e)
{
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
// Manually register the event-handling method for the ItemCreated
// event of the DataGrid control.
this.dlLine.ItemCreated += new System.Web.UI.WebControls. DataListItemEventHandler(this.dlLine_ItemCreated);
}
Lastly I added the actual dlLine_ItemCreatedEvent
Code:
protected void dlLine_ItemCreated(object sender, DataListItemEventArgs e)
{
TextBox tx = (TextBox)(e.Item.FindControl("txtorderLine"));
if (l != null)
{
if (Request.QueryString["OrderId"] == null)
{
l.Text = "your value";
}
}
}
When I run my code (i.e manually triggering btnLineAdd_Click) it doesnt work as textbox tx is null, but I did find that this would work if the datalist has actually been bound i.e. if it opens an existing order that already has values that populate dlLine.
Basically my question is how can I access/reference a textbox control inside a datalist if it hasn’t yet been populated (i.e. bound to a datasource).
Thanks