Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie Query - How do you access a textbox inside a Datalist/Repeater?

Status
Not open for further replies.

sunil128

Programmer
Mar 20, 2008
45
0
0
GB
Hi all, I am currently passing a number of parameters from one webform to another, these parameters are then used to set/populate textboxes & DDL’s on the receiving webform. This all works fine, however I do not know how to write a value to a textbox that is part of a datalist/repeater? So basically how do I access this textbox?

Can anyone tell me how to do this in very simple terms? I know findcontrol is used but I still cant get my head around the concepts involved.

Thanks
Sunil
P.s. app is a web application in VS2005 using C# code behind.
 
Code:
<asp:reapeater ... ItemDataBound="PopulateTextbox">
...
</asp:repeater>
Code:
protected void Page_Load(..)
{
   Repeater.DataSource = GetData();
   Repeater.DataBind();
}
protected void PopulateTextbox(object sender, RepeaterItemDataBoundEventArgs e)
{
   //if it the row you want
   TextBox ctrl = e.Item.FindControl("name of textbox") as TextBox;
   if(ctrl != null) ctrl.Text = "your value";
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Many Thanks Jason, that was exactly what i was after!

But how exactly do i call the PopulateTextbox method? i.e what parameters am i passing?
 
the control takes care of that automatically when you call Repeater.DataBind().

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Hi Jason, im still struggling trying to understand this, can you explain exactly whats going on with your code suggestion please.

For example:
Code:
protected void PopulateTextbox(object sender, RepeaterItemDataBoundEventArgs e)
{
   //if it the row you want
   TextBox ctrl = e.Item.FindControl("name of textbox") as TextBox;
   if(ctrl != null) ctrl.Text = "your value";

- What actually calls PopulateTextbox & what are the parameters? (i didnt understand your previous answer)
- Is "e" the name of my datalist?
- Is Item neccesary?
- Does the line { "if(ctrl != null) ctrl.Text = "your value" } actually update the datalist textbox or the textbox called 'ctrl'?

Ive been looking at other examples on the net and they refer to using the datalist_ItemCreated event. Do i need this?
 
Normally you leave the event name the same, so, in the code, the Repeater_ItemDatabound event gets called each time a row is bound to the Repeater. In Jason's example, in the HTML code, he told it to call the PopulateTextBox sub instead:
Code:
<asp:reapeater ... ItemDataBound="PopulateTextbox">
...
</asp:repeater>

So, instead of Repeater_ItemDataBound() being called for each row, PopulateTextBox is being called.

 
Apologies for my lack of understanding here, but can someone explain all the concepts of this me to in laymans terms please (proper novice me!)

Please note that all im trying to do i pass a text string from one page to another and then paste this text value into the first instance of the textbox in the datalist, i.e. before the datalist is even bound to the data!
 
...just to add, the datalist in question is used to both display the data and also for user to input data directly & then save.
 
Apologies for my lack of understanding here, but can someone explain all the concepts of this me to in laymans terms please (proper novice me!)
you need to understand the tools your working with. research the repeater control, the events and members. review examples online and if you need help with specific issues then create a post.

you are correct, what you want is easy. you just need to understand how to get to it.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
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


 
you can't. nothing exist to be bound to.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
We have found a solution/workaround to this problem that doesnt involve trying to access the textbox using FindControl. Thanks everyone for their help & suggestions anyway.
 
Post your workaround.... as your complete question is a little confusing. Would like to see the result of what you wanted.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

- The Complete IT Community
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top