• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 500
  • Last Modified:

Gridview assign ID on text boxes

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
Mr_Shaw
Asked:
Mr_Shaw
  • 11
  • 9
1 Solution
 
biodoomsCommented:
Could you post the code so I can give you an direct example for your case?
0
 
Mr_ShawAuthor Commented:
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
 
biodoomsCommented:
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
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.

 
Mr_ShawAuthor Commented:
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
 
biodoomsCommented:
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
 
Mr_ShawAuthor Commented:
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
 
biodoomsCommented:
can you add the code where you dynamically add your textboxes?
0
 
Mr_ShawAuthor Commented:
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
 
biodoomsCommented:
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
 
biodoomsCommented:
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
 
biodoomsCommented:
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
 
Mr_ShawAuthor Commented:
Something is up with the code

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


0
 
biodoomsCommented:
What is the problem?
0
 
Mr_ShawAuthor Commented:
Textbox UserIDtb = e.FindControl("UserIDtb"); does not work.

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

but still no joy.

0
 
biodoomsCommented:
sorry. Please try this.

Textbox UserIDtb = (Textbox)e.FindControl("UserIDtb");
0
 
Mr_ShawAuthor Commented:
TextBox UserIDtb = (TextBox)e.FindControl("UserIDtb");

FindControl is not part of 'e'
0
 
biodoomsCommented:
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
 
Mr_ShawAuthor Commented:
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
 
biodoomsCommented:
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
 
Mr_ShawAuthor Commented:
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 11
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now