?
Solved

Gridview assign ID on text boxes

Posted on 2008-10-31
20
Medium Priority
?
498 Views
Last Modified: 2013-12-17
I have been told I need to assign ID's to my textboxes in my gridview. How do I do this, the textboxes are created dynamicly by asp.net when I click the gridveiw edit button?
0
Comment
Question by:Mr_Shaw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 9
20 Comments
 
LVL 3

Expert Comment

by:biodooms
ID: 22849003
Could you post the code so I can give you an direct example for your case?
0
 

Author Comment

by:Mr_Shaw
ID: 22849023
The code which I am using to try and captue the updated entries is:

protected void edit_record_updating(object sender, GridViewUpdateEventArgs e)
    {
       
        update_class update_records_method = new update_class();
        update_records_method.Record_ID = ((TextBox)user_records.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString();
        update_records_method.Logon_Name = WindowsIdentity.GetCurrent().Name.ToString();
        update_records_method.Record_Class = ((TextBox)user_records.Rows[e.RowIndex].Cells[4].Controls[0]).Text.ToString();  
        update_records_method.Record_Name = ((TextBox)user_records.Rows[e.RowIndex].Cells[6].Controls[0]).Text.ToString();
}

The code is just not picking up the updated values from the gridview.
0
 
LVL 3

Expert Comment

by:biodooms
ID: 22849078
I would recommand you to do a findcontrol.

For a start your controlls need an ID. So if you make a textbox dynamic it would be something like this:

Textbox ProductNametb = New Textbox();
ProductNametb.ID = "ProductNametb";

Then to get the text on an record update you do:

Textbox ProductNametb = e.FindControl("ProductNametb");
if (ProductNametb != null)
{
    update_records_method.Product_Name = ProductNametb.Text;
}

Sorry if there are some slightly code problems since I typed it very fast. Remember that an ID has to be unique. Using findcontrol has the advantage that all controls can be changed in order since the control can always be found by ID.

I hope this is what you meant.

With kind regards,

Kevin Hendricks.
0
More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

 

Author Comment

by:Mr_Shaw
ID: 22849115
I don't need to create any new textboxes since it is done dynamicaly by the gridview.
I have included a screen shot of what i mean below
gridview.bmp
0
 
LVL 3

Expert Comment

by:biodooms
ID: 22849134
what I meant was not for you to make an new textbox but to add the ID property to the textbox you make like this

Textboxname.ID = "Textboxname";

If you already do this than that's fine but you need an ID on the textbox. Now when the update is triggerd get the textbox from the update row like this:

Textbox Textboxname= e.FindControl("Textboxname");

Now do a check if the control is not null ( so it has been found)

if (Textboxname != null)
{

Now finally get the text from the textbox and use the text property to get the information from the textbox.

update_records_method.Logon_Name = Textboxname .Text;

So it looks like this:

Textbox Textboxname= e.FindControl("Textboxname");
if (Textboxname != null)
{
    update_records_method.Logon_Name = Textboxname .Text;
}


With kind regards,

Kevin Hendricks
0
 

Author Comment

by:Mr_Shaw
ID: 22849236
I am really sorry to say that I just can't implement your code.

I just can't assign the ID to the text boxes. Where do you place your code. How does the code know which textbox to assign the ID since there are many textboxes in the gridview
0
 
LVL 3

Expert Comment

by:biodooms
ID: 22849258
can you add the code where you dynamically add your textboxes?
0
 

Author Comment

by:Mr_Shaw
ID: 22849308
I do not add the textboxes. The gridview automatically creates the when I click on the edit button.

I am sorry i used the word dynamic, it was missleading.
0
 
LVL 3

Expert Comment

by:biodooms
ID: 22849401
That explains alot. The reason that I never have these problems is because I almost always make the rows myself. So instead of autogeneraterows on I define an updatetemplate. But I dont have any code with me now so I will search the web for an example.

You can use the findcontrol property once you have defined textboxes in the front code.

Give me a second to search an example
0
 
LVL 3

Expert Comment

by:biodooms
ID: 22849477
You really make me do alot:P I typed this a bit fast but this is how i would build it.

<asp:gridview id="AuthorsGridView" autogeneratecolumns="False" runat="server">
	<columns>
		<asp:templatefield>
            <itemtemplate>
				<asp:button id="UserIDlbl" text="Edit" runat="server" commandname="edit" /> 
            </itemtemplate>
			<edittemplate>
				<asp:button id="UserIDtb" text="Update" runat="server" commandname="update" />
				<asp:button id="UserIDtb" text="Cancel" runat="server" commandname="cancel" />
			</edittemplate>
        </asp:templatefield>
        <asp:templatefield headertext="User_ID">
            <itemtemplate>
				<asp:label id="UserIDlbl" text='<%# Eval("User_ID") %>' runat="server" /> 
            </itemtemplate>
			<edittemplate>
				<asp:textbox id="UserIDtb" text='<%# Eval("User_ID") %>' runat="server" />
			</edittemplate>
        </asp:templatefield>
		<asp:templatefield headertext="record_name">
            <itemtemplate>
				<asp:label id="RecordNamelbl" text='<%# Eval("record_name") %>' runat="server" /> 
            </itemtemplate>
			<edittemplate>
				<asp:textbox id="RecordNametb" text='<%# Eval("record_name") %>' runat="server" />
			</edittemplate>
        </asp:templatefield>
		<asp:templatefield headertext="record_class">
            <itemtemplate>
				<asp:label id="RecordClasslbl" text='<%# Eval("record_class") %>' runat="server" /> 
            </itemtemplate>
			<edittemplate>
				<asp:textbox id="RecordClasstb" text='<%# Eval("record_class") %>' runat="server" />
			</edittemplate>
        </asp:templatefield>
		<asp:templatefield headertext="medium">
            <itemtemplate>
				<asp:label id="Mediumlbl" text='<%# Eval("medium") %>' runat="server" /> 
            </itemtemplate>
			<edittemplate>
				<asp:textbox id="Mediumtb" text='<%# Eval("medium") %>' runat="server" />
			</edittemplate>
        </asp:templatefield>
		<asp:templatefield headertext="volume">
            <itemtemplate>
				<asp:label id="Volumelbl" text='<%# Eval("volume") %>' runat="server" /> 
            </itemtemplate>
			<edittemplate>
				<asp:textbox id="Volumetb" text='<%# Eval("volume") %>' runat="server" />
			</edittemplate>
        </asp:templatefield>
		<asp:templatefield headertext="location_type">
            <itemtemplate>
				<asp:label id="LocationTypelbl" text='<%# Eval("location_type") %>' runat="server" /> 
            </itemtemplate>
			<edittemplate>
				<asp:textbox id="LocationTypetb" text='<%# Eval("location_type") %>' runat="server" />
			</edittemplate>
        </asp:templatefield>
		<asp:templatefield headertext="duplication">
            <itemtemplate>
				<asp:label id="Duplicationlbl" text='<%# Eval("duplication") %>' runat="server" /> 
            </itemtemplate>
			<edittemplate>
				<asp:textbox id="Duplicationtb" text='<%# Eval("duplication") %>' runat="server" />
			</edittemplate>
        </asp:templatefield>
		<asp:templatefield headertext="include_persona">
            <itemtemplate>
				<asp:label id="IncludePersonalbl" text='<%# Eval("include_persona") %>' runat="server" /> 
            </itemtemplate>
			<edittemplate>
				<asp:textbox id="IncludePersonatb" text='<%# Eval("include_persona") %>' runat="server" />
			</edittemplate>
        </asp:templatefield>
	</columns>
</asp:gridview>

Open in new window

0
 
LVL 3

Expert Comment

by:biodooms
ID: 22849503
Then on the update you do what I showed you before. Below is the case for User_ID (UserIDtb textbox)

Textbox UserIDtb = e.FindControl("UserIDtb");
if (UserIDtb != null)
{
    update_records_method.User_ID = UserIDtb.Text;
}
0
 

Author Comment

by:Mr_Shaw
ID: 22850001
Something is up with the code

Textbox UserIDtb = e.FindControl("UserIDtb");
if (UserIDtb != null)
{
    update_records_method.User_ID = UserIDtb.Text;
}


0
 
LVL 3

Expert Comment

by:biodooms
ID: 22850045
What is the problem?
0
 

Author Comment

by:Mr_Shaw
ID: 22850069
Textbox UserIDtb = e.FindControl("UserIDtb"); does not work.

i changed it to TextBox UserIDtb = e.FindControl("UserIDtb");

but still no joy.

0
 
LVL 3

Expert Comment

by:biodooms
ID: 22850106
sorry. Please try this.

Textbox UserIDtb = (Textbox)e.FindControl("UserIDtb");
0
 

Author Comment

by:Mr_Shaw
ID: 22850152
TextBox UserIDtb = (TextBox)e.FindControl("UserIDtb");

FindControl is not part of 'e'
0
 
LVL 3

Expert Comment

by:biodooms
ID: 22850188
the problem is that i dont have visual studio on this machine. You need the row and then do an findcontrol on it. So please try this: (TextBox)

TextBox UserIDtb = (TextBox)user_records.Rows[e.RowIndex].FindControl("UserIDtb");
0
 

Author Comment

by:Mr_Shaw
ID: 22850232
I tried,

TextBox UserIDtb = ((TextBox)user_records.Rows[e.RowIndex].FindControl("UserIDtb")).Text.;

but  it says it annot implicitly convert type 'string' to 'System.Web.UI.WebControls.TextBox'
0
 
LVL 3

Accepted Solution

by:
biodooms earned 2000 total points
ID: 22850284
yes that is because you do:

TextBox UserIDtb = ((TextBox)user_records.Rows[e.RowIndex].FindControl("UserIDtb")).Text.;

But you declare an  textbox on the left so u cant use the property .text try this like i said:

TextBox UserIDtb = (TextBox)user_records.Rows[e.RowIndex].FindControl("UserIDtb");
0
 

Author Closing Comment

by:Mr_Shaw
ID: 31511978
I will have to play around with the code a bit more. It all works without any errors, but it is still not picking up the new edited values.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question