Edit Bound DataGrid row w/out entering Edit Mode???

Hi,

I am not sure if this is possible or not, but I think it may well be...

I have a databound grid, which, has just a single field which needs updating. Instead of making the user click edit to enter the edit mode just to change a single field in a single record, I wondered if it is possible to use a template field on the GridView with a textbox in. The user can then enter text for the record, in this case an Account Number, and click edit to update the record.

I have written out the GridView and have a template field with the textbox in absolutely fine. Now I am trying to get the value from the textbox, but can't. No matter which method I try be it, onSelectedIndexChanged, onRowEditing, OnRowUpdating, OnRowUpdated I simple get back an empty string from the textbox. I can happily retreive the DataKey value, so I know what record I need to update and I know that I am getting the text value of the textbox, but, it just is not seeing that the User has typed in text, so I am getting an empty string.

Anybody have any ideas?

Code follows:

GridView code:

<asp:GridView ID="GridView2" runat="server" Width="400px" AutoGenerateColumns="False" style="border:none 0px" BorderStyle="None" BorderWidth="0px" CellPadding="2" PageSize="50" DataKeyNames="Customer_Id" OnSelectedIndexChanged="EditSelected">

<PagerSettings Position="TopAndBottom" />
<FooterStyle BorderStyle="None" />
<Columns>
<asp:BoundField DataField="Col_CompanyName" />
<asp:TemplateField HeaderText="Collection Address">
<ItemStyle Width="375px" HorizontalAlign="Left" />
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# "<strong>" & Container.DataItem("Col_CompanyName") & "</strong>, " & Container.DataItem("Col_add_1") & ", " & Container.DataItem("Col_add_2") & ", " & Container.DataItem("Col_add_3") & ", " & Container.DataItem("Col_Town") & ", " & Container.DataItem("Col_County") %>'></asp:Label>

</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />

</asp:TemplateField>
<asp:TemplateField HeaderText="AccID">
<ItemTemplate>

<asp:CheckBox ID="CheckBox1" runat="server" />
<asp:TextBox ID="AccNo" Font-Size="x-small" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

<asp:CommandField SelectText="[- EDIT -]" ShowSelectButton="True" >
<ItemStyle Width="70px" />
</asp:CommandField>


<asp:CommandField ShowEditButton="True" />


</Columns>
<RowStyle BackColor="#E0F0D9" />
<PagerStyle BorderStyle="None" BorderWidth="0px" />
<HeaderStyle BorderStyle="None" />
<AlternatingRowStyle BackColor="#CCCCCC" />
</asp:GridView>

 

My Code behind:

Protected Sub EditSelected(ByVal sender As Object, ByVal e As System.EventArgs)

Dim CustID
Dim CustACC

CustACC = DirectCast(GridView2.SelectedRow.Cells(2).FindControl("AccNo"), Textbox).text
CustID = GridView2.SelectedDataKey(0)

End Sub

 

I am simply trying to get the textbox value at the moment, hence, the fairly sparce code behind. I have also tried the GridView2.Rows(e.NewEditIndex) but still no luck..

 

Thanks in advance

RNUK

0
RNUK
6/29/2007 4:39:26 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

13 Replies
876 Views

Similar Articles

[PageSpeed] 9
Get it on Google Play
Get it on Apple App Store

Hi, RNUK :

It is possible to achieve this.

Here is an example

<%--click this button to update datas--%> 

<asp:ButtonField CommandName="Update" Text="Update" />

<%--this is the teamplte containning a textbox--%>

<asp:TemplateField>

<ItemTemplate>

<asp:TextBox runat="server" ID="textbox" Text='<%# Bind("name") %>' ></asp:TextBox>

</ItemTemplate>

</asp:TemplateField>

My UpdateCommand of SqlDataSource1 is

UpdateCommand="update basicInfo set name=@name where number=@number" 

Set the DataKeyNames="number".

Without any addtional codes added, it works well.

You can change it according to your project.

If you have any problem about it, do not hesitate to let me know.


Sincerely,
Techie Zhang
Microsoft Online Community Support
0
Techie
7/2/2007 6:36:22 AM

Hi:

  This works for me.

protected void GridView4_SelectedIndexChanged(object sender, EventArgs e)
    {
        TextBox t = (TextBox)GridView4.SelectedRow.FindControl("AccNo");
        Response.Write(t.Text + "<br>");
    }

    <asp:GridView AutoGenerateSelectButton="true" ID="GridView4" runat="server" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="GridView4_SelectedIndexChanged" >
 <Columns>
 <asp:TemplateField>
 <ItemTemplate>
 <asp:TextBox ID="AccNo" Font-Size="x-small" runat="server"></asp:TextBox>
 </ItemTemplate></asp:TemplateField></Columns>
    </asp:GridView>

  At first, please set a breakpoint at EditSelected to make sure this event fires.

  Secondly, try this:

Dim CustACC as TextBox

CustACC = DirectCast(GridView2.SelectedRow.FindControl("AccNo"), Textbox).text

If it still doesn't work, please inform us.

Regards


Sincerely,
Allen Chen
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Allen
7/2/2007 6:55:39 AM

Thanks for the reply Techie Zhang.

I can't run a direct SQL call from the code as all data is passed through SOAP. However, I am trygin to do this with a linik button rather than an update button so I'll give that a go! Thanks

0
RNUK
7/2/2007 8:10:33 AM

Thanks Alllen. I will give it a shot.

Does that code translate well to vb.net, as I saw this type of solution elsewhere and tried it but in C#, but, my code kept complaining about the TextBox t = (TextBox) at the beginning of the code?

0
RNUK
7/2/2007 8:22:20 AM

Hi:

  VB version:

 Dim t as TextBox  =GridView4.SelectedRow.FindControl("AccNo")
 Response.Write(t.Text + "<br>")

Regards


Sincerely,
Allen Chen
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Allen
7/2/2007 8:38:40 AM

Thanks for both answers, I have tried both, but, on each occasion I get a handle on the textbox, but, the text typed is not parsed at that point still...

Any ideas?

Thanks in advance.

0
RNUK
7/2/2007 9:17:42 AM

Hi:

  I'm not sure why this doesn't work on your machine. Could you test it in a new project?

      <asp:GridView AutoGenerateSelectButton="true" ID="GridView4" runat="server" DataSourceID="SqlDataSource1" >
 <Columns>
 <asp:TemplateField>
 <ItemTemplate>
 <asp:TextBox ID="AccNo" Font-Size="x-small" runat="server"></asp:TextBox>
 </ItemTemplate></asp:TemplateField></Columns>
    </asp:GridView>


 Protected Sub GridView4_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView4.SelectedIndexChanged
        Dim t As TextBox = GridView4.SelectedRow.FindControl("AccNo")
        Response.Write(t.Text + "<br>")


    End Sub

  What's the error description and which line does the error point to?

Regards


Sincerely,
Allen Chen
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Allen
7/2/2007 9:30:39 AM

This is the thing, it doesn't actually generate an error.

I have stepped through the whole sub and it runs every line of code without error, but, it just doesn't see the text I have entered.

I have tried a new project linked to a local DB through the DataSourceID as in your priject and it works fine...?

Coudl this be a problem with trying to run from a remote DB using SOAP? I don't see why that would effect the ability to grab a textbox value though, but I really am clautching at straws now.

0
RNUK
7/2/2007 9:44:31 AM

Yep, thats the problem!

If I add a local DB and use DataSource in the GridView to bind it, then I get the textbox value returned. As soon as I bind the data in the code behind... It stops.

I wonder if the page_load code is being fired before the EditSelected code, thus, returning a blank field (as the field in the DB is blank to start with..)

0
RNUK
7/2/2007 10:07:19 AM

Hi:

 

RNUK:
I wonder if the page_load code is being fired before the EditSelected code

 

  Yes. You can set two break points in these two event handlers to see which one fires earlier.

  What's the extra code in your Page_Load?

  Then my guess is that you bind your GridView in every post back. So try this:

if(!IsPostBack){//populate your GridView here.}

If it doesn't work, please inform us.

Regards


Sincerely,
Allen Chen
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Allen
7/2/2007 10:11:05 AM

Yep!

Pageload is firing first and as my grid is bound on postback, the gridview is re-binding before I get to the routine to pick up the textbox, so, I am getting the blank field...

So now I have to supress the pageload code when the user hits the selct button...?

0
RNUK
7/2/2007 10:33:11 AM

Hi:

  I've edited my previous post. Have you tried this in Page_Load?

if(!IsPostBack){//populate your GridView here.}

If it doesn't work, please inform us.

Regards


Sincerely,
Allen Chen
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Allen
7/2/2007 10:45:28 AM

Works a treat now thanks.

I have had to move some stuff about so that I can wrap the data bind in a "if Not page.ispostback" statement

Works a charm, thank you for your help on this. Very much appriciated!!!!!

 

0
RNUK
7/2/2007 11:36:02 AM
Reply:

Similar Artilces:

Add new controls to a gridview row; when editing that row using the edit row feature in the gridview control.
Hi all  I have a questions that looks hard / complex to me, and i know you can help me in this. Q {  I have row in a Gridview and i have an edit option to it. I press the edit button, to edit the values in that row and update data all the way back to the database. Now when i press the edit button, i want a drop down list to be displayed that contain values for a particular field in my grid view. (for now only plain text boxes appear.) } I hope you understand my question, all i need to do is replace the text box controls with a dropdown list when i am editing a row in ...

GridView control enters edit mode only when I click Edit link twice
Hi all..I have a GridView control in an ASP.NET 2.0 page. I added a column that contains edit, update, cancel buttons. Because I did that, I had to handle RowEditing event or an exception was thrown.Well... this is the body of the RowEditing event:protected void gvParametros_RowEditing(object sender, GridViewEditEventArgs e){      gvParametros.EditIndex = e.NewEditIndex;}The fact is that when page is loaded and I click the edit link, page is refreshed but GridView doesn't enter edit mode. When I press edit link again, GridView enters edit mode.Any help to solve this behavior w...

Gridview Edit go to new page and enter Edit mode on a formview control.
Okay, so far I have the edit button go to a page called update.aspx. And have the ID selected. However, I don't want it to be in selected view, I want it to go to the update.aspx page in edit mode not selected mode. Is this possible? You can add one hyperlink and set its path to update.aspx?Id=(selectedId)on update.aspx read Id's value from querystring and do updation - Hardik (Software Developer,Surat,India)Please remember to click "Mark as Answer" on this post if it helped you. You lost me at the "do updation part" so far I have a the EDIT link go to ...

Listview control enter edit mode rows are duplicated
Hi,  I have a problem with a listview control. When I enter edit mode, the rows in my listview are duplicated. The following is a snippet of my code behind:     Public Sub page_load(ByVal sender As Object, ByVal e As EventArgs)              If Not Page.IsPostBack Then                                         ...

Datagrid Editing
Hello all... I have a Datagrid with an Edit button. User clicks it and is able to edit the row... No problems there. However, I'd like it if the datagrid only showed the row being edited when a user clicked the edit button. Anyone know a way to do this? As you know, the datagrid is a built in .net control. As far as I know, there is no way to do this with just a datagrid alone. I think you would have to have the edit button on each row in the datagrid with an onclick handler in the codebehind. This onclick would get the row position, hide the grid, post data from this row to a se...

Edit control is not loaded in edit mode
I have a problem that the dynamic data field edit control (such as TEXT_edit.ascx) is not loaded when in edit mode. Instead, the normal view control is loaded (such as Text.ascx). In debug I can determina the page mode is truly edit mode. Please help! Could you provide some more context: is this in a custom page with a custom column set or one of the default scaffold pages? Are you using a UIHintAttribute? Make sure the names of the display and edit versions match.Marcin DoboszSDE, ASP.NET Team, MicrosoftRead my blog This is just a thought are you using Views rather than ...

Making editable datagrid in which all rows are editable
I find Scott Mitchel article http://aspnet.4guysfromrolla.com/demos/dgExample35.aspx Which displays all the rows in editable mode. Now is there any way to know only that which rows have been changed. I mean if I change only one row in the datagrid so I only need to get that one row. Since I am sending all in the database and I dont want to send all the rows back to the database. this will be bad for performance. any ideas ??? HighOnCodingWanna get high! Yes, excellent article that one...The idea of that demo was to illustrate how you can edit ALL the data in a datagrid; s...

editing the size of a textbox in edit mode in datagrid
while am clicking on edit button of a datagrid textboxs are opening in edit mode.but they are too big and i wanna fix their length.how can i do that???plz help me out............ Use column templates and custom textboxes so you can set them CSS styles.Don't forget to mark solution providing post as "Answered".It helps others to find correct solutions!Also visit my ASP.NET blog! Instead of <BoundColumn> use <TamplatedColumn> - there you can add <asp:Textbox ...> and specify its width http://authors.aspalliance.com/aspxtreme/webforms/controls/addin...

Controls in DataGrid (During edit mode)
When editing a selected row in a DataGrid, I want to use a dropdownlist control to provide users with restricted options to modify the value with (and also to mark the current value as selected). So, I placed the <asp:dropdownlist> tag within the <edititemtemplate> tag. Then I placed code to populate the dropdownlist within the OnEditCommand event of the DataGrid. However, once I click on any item within the grid to modify values, the dropdownlist object is null (causing reference exception). When I remove the dropdownlist population code from the OnEditCommand event handler,...

Size of edit text box when datagrid is in edit mode
I have a datagrid that allows me to put it in edit mode by clicking on the edit button. The editing feature works fine but my problem has to do with the size of the text boxes placed in the grid when it is in edit mode. The text boxes are much larger (width wise) than they need to be. At most I would need to enter a 3 digit number in them but they are big enough to enter a 10-15 digit number. Is there a way I can control the width of the textbox that appears when the datagrid is placed into edit mode? Thanks, David Hi, You can do this by setting the column property of ...

Editable/Not Editable
Hello All, I have some webform(.aspx page) in which i have some controls (for input) and i would like to make that screen editable/not editable based on some condition. how can i do that.  the whole intent is user can read the information but can't change.  shiva kumar If you want to make the controls editable/non-editable, you can set their ReadOnly property to true or false accordingly. What exactly do you imply by making the screen editable/non-editable?Thanks,MaxLet Me Google That For You! i mean to say i would like to show the current values but user should not mod...

To edit or no to edit
Jon, Can I edit, whatever I've written so far, so that it doesn't look like I wrote utterly crap?-) -- Pieter "Linux is like living in a teepee. No Windows, no Gates, Apache in house." -- Usenet signature Pieter Zijlstra wrote: > Jon, > > Can I edit, whatever I've written so far, so that it doesn't look like > I wrote utterly crap?-) Oh boy, the edit button vanished from the web-forum ... ;-) -- Pieter "Humor is also a way of saying something serious." -- T. S. Eliot (1888 - 1965) Pieter Zijlstra wrote: ...

Any trick to edit any colmun in a TListview control report mode? [Edit]
I mean every subitems from the list (report mode), not just the caption, editing done within the list itself. Or any third-party TListView derived from Delphi TListView that do allow it (not TVirtualStringTree). Thanks. <richard poulin> wrote in message news:104692@forums.codegear.com... > I mean every subitems from the list (report mode), not just the caption, > editing done within the list itself. How to use the Build-in Editor of TListView to Edit SubItems http://cc.embarcadero.com/Item/23873 -- Remy Lebeau (TeamB) > {quote:title=Remy Lebeau (TeamB) wrote:...

How do you edit a datagrid row, but not in the datagrid?
I'm using a Datagrid to display links from an Access database. The URL is incorporated in to the displayed link, of course. But I would like to click an edit button and have an edit form appear beside the datagrid columns, for that row only, allowing me to edit everything, including the URL, instead of editing directly in the datagrid. I would also like the ability to add new rows from the form. Can anyone tell me how to do this? Thanks, Gary I should have added that I'd like the edit form to appear only when the edit button is clicked... G. <asp:DataGrid id="D...

Web resources about - Edit Bound DataGrid row w/out entering Edit Mode??? - asp.net.presentation-controls

Want to preview our new DataGrid for Xamarin.Forms?
tl;dr Zumero.DataGrid is a Xamarin.Forms control for displaying data in rows and columns. If you would be interested in testing and previewing ...

Mad King DataGrid
This post is about scrolling in WPF and the egocentric DataGrid control. I’ll give the project background but if all you’re interested in is ...

Stack Overflow
Stack Exchange log in - careers - chat - meta - about - faq Questions Tags Users Badges Unanswered Ask Question Top Questions interesting 295 ...

Microsoft Silverlight - Wikipedia, the free encyclopedia
Microsoft Silverlight is an application framework for writing and running rich Internet applications , with features and purposes similar to ...

Flash Builder Plug-in for SAP Netweaver Gateway - Adobe Labs
Search Navigation Labs Home Wiki Home Technologies Alchemy Blueprint Camera Profiles and DNG Profile Editor Cirrus ColdFusion 10 DNG Codec Fireworks ...

Casaba Security » Products » X5s Testing Tool For Finding XSS Vulnerabilities
Security analysis, consulting, development and testing partners who understand your business strategy.

CodeGuru - Microsoft developers related ideas, articles, tips, tricks, comments, downloads, and so much ...
CodeGuru is where developers can come to share ideas, articles, questions, answers, tips, tricks, comments, downloads, and so much more related ...

Caliburn Micro: WPF, Silverlight, WP7 and WinRT/Metro made easy.
A small, yet powerful framework designed for WPF, Silverlight and WP7. It implements a variety of UI patterns for solving real-world problems. ...

Quick Answers - CodeProject
Questions - Free source code and tutorials for Software developers and Architects.; Updated: 11 Jul 2012

license-discuss@opensource.org: 11636: first draft of license proliferation committee report
FYI,the first draft report of the OSI's License Proliferation Committee.To join the OSI's license proliferation discuss email list, send anemail ...

Resources last updated: 12/8/2015 6:49:53 AM