Solved

Gridview assign ID on text boxes

Posted on 2008-10-31
20
492 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
  • 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
 

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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 500 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now