I am currently working with a GridView embedded in a custom user control. The Command field is the default one generated by the GridView itself, and all the buttons work, save for the Update button. The RowUpdating event fires, but the row itself is not in edit mode, so any code I use to attempt to find controls within the row (i.e. the text boxes for the updated values) returns null values.
The only thing I've been able to find so far is this: https://forums.asp.net/t/1475154.aspx
The provided "answer" seems to point at the CommandName value, but I thought the whole point of using the default generated CommandField meant that was handled.
Any help/thoughts on the issue would be appreciated.
Turns out it was just an issue with the page post-back, and when each individual user control fired its DataBind().
Related
I'm using a form (FormView) with databinding (ObjectDataSource) and all my input fields are bound by using '<%# Bind("field") %>'.
Everything works fine, but I have two problems (which I found various hints about like using this.Validate() or .EndEdit() - but none seem to work):
Entries are only saved after leaving the input field so it looses focus
Let's say I have a textbox with an ID of Name and enter "George". When I would tab to the next textbox or when I click somewhere else and click save - everything is saved. But when I keep the focus in the textbox the value is not saved. Why is this happening? What magic can I use to circumvent this (JavaScript to the rescue?).
I set a textbox's field value (element.value) via Javascript (upon selecting something in a combobox).
The same problem as above applies, only when I give the textbox focus and tab out the value is saved. This creates the problem that I only want the user to choose something in the combobox (the textbox is updated accordingly) and move on - I don't want the user to click into the textbox afterwards and tab out again.
Edit:
The second problem I resolved now by setting the focus onto my textbox via Javascript (textbox.focus();) and right after set the focus back to the combobox (combobox.focus();) and that does the trick - this seems fairly hackish to me, doesn't it?
I'm assuming this is fairly common, but my mighty Google fu hasn't help me find a simple solution.
A similar issue can crop up in Winforms development when working with DataGridView controls. I typically attach some logic to the submit button's Click event to cause the DataGridView to validate. I suspect a similar solution would work for you here.
I just created a basic ASP.NET website using Microsoft's walkthrough here. It has one page with a form that takes in some text input, runs a database query based on that, and results the results in a GridView. I added the EmptyDataText property to my GridView to explicitly show users when their search returns no results.
It all works as expected with one exception:
IIS shows my EmptyDataText of "No results found." even before the search form is submitted.
This defeats my purpose behind using EmptyDataText, which is to indicate to the user that the webpage successfully submitted their search but found no results, as opposed to took their search and threw it into the ether.
For example, a user who searches for something that cannot be found will see "No results found." both before and after their search, as opposed to nothing before and "No results found." after. The former behavior gives the impression that the search didn't work.
How can I configure my GridView to show the EmptyDataText only after the search form is submitted?
you are using sqldatasource that will bind automatically while loading the page. Bind the gridview programatically while clicking the search button
Don't bind your GridView until after the user has initiated a search. When you bind your GridView with a datasource that has 0 records, then the EmptyDataText will be displayed.
Chances are that you are binding it on Page_Load.
EDIT
Wherever a DataBind() is performed in your code (other than the action handler), remove it. Your DataBind() should only occur in the handler that receives the user action.
If Visual Studio is doing some voodoo behind the scenes with automatic binding, you can always default the grid to invisible. Make it visible when the user initiates a search.
Just don't databind it before the search, it will effectively be completely invisible until then.
UPDATE: maybe you are using a DataSourceID (which databinds automatically)?
UPDATE 2: First off, for what ever reason was the downvote (at least have a decency to leave a comment)? If it weren't for me the OP would still not have known where the problem lies - and secondly, just remove the DataSourceID property from the declaration, and set it back from codebehind when the user makes a search (you might want to call the GridView DataBind() method manually after that, but only if it doesn't do that on its own - try it without first).
As the other answers suggested, the GridView is being bound before the user makes a search because it has a DataSourceID attribute. As explained on MSDN, this attribute causes the GridView to automatically bind to the specified source:
To bind to a data source control, set the DataSourceID property of the GridView control to the ID value of the data source control. The GridView control automatically binds to the specified data source control and can take advantage of the data source control's capabilities to perform sorting, updating, deleting, and paging. This is the preferred method to bind to data.
To get the behavior I was looking for, I removed that attribute from the GridView and instead added an OnClick attribute to the submit button for my search form. The OnClick attribute refers to a method BindGridView that gets called only when the user submits the form.
All this method does is populate the DataSourceID with the same value it had before as an attribute:
public void BindGridView(object sender, EventArgs e)
{
GridView1.DataSourceID = "DataSourceID1";
}
I'm pretty new to c# and Winforms and I'm wondering what is the best approach to the following screen design.
I have a window that contain a Datagrid wich would be read-only. Beneath the grid, I have the detail of the records in differents fields (textbox, combobox, checkbox).
What I want is that when the user click on an item in the datagrid, the data will be shown in the detail fields.
That part is pretty easy, but I want to be able to update the fields automatically, wich means, I would prefer to not have to press a Save button.
Let's say that I click an item in the datagrid, change some value in the detail fields and the I click on another item in the datagrid, then I also want to perform some validation and calculation before the record get updated.
What I was thinking at first was to get the button for "new", "edit", "save" action and lock and unlock the fields accordingly and keep a flag to know if i need to insert or update the data, but then I realized that I would prefer to not have thoses button and have the save performed automatically.
Is there any sample somewhere that does what I want?
Also, would you guys using the built-in databinding functunality or just use a dataset object in code?
Pretty common scenario.
On selected row change of grid you know which datarow you shoul bind to the other controls. when same event happens again you validate and save or cancel in case of errors.
You can make use of DataGridView.CellEndEdit Event to get the new value and DataGridView.CellBeginEdit event to get the old value and update your data if there is any change
I have a textbox in one grid-view column where upon entering a particular value and losing focus of the textbox, should post to the server to get the text validated through a server-side method. If the entry is valid, a result set to fill rest of row cells would be returned, else the bgcolor of the textbox needs to be changed to Red.
I tried posting back through the obvious way, i.e. making the textbox's autopostback as true and wiring up a server-side OnTextChanged event handler to validate the entered value.
It is working with this setup, but is also affecting the remaining page controls behaviour. For example, if I click a button in some other grid after entering some text in the textbox, the OnTextChanged handler gets called thus preventing the button's click event, which I also wish to call to execute its functionality.
Kindly suggest what alternatives/corrections I should carry out to enable textbox content server-side validation plus making the other controls/updatepanels work as expected.
Me dumb. I tried everything from creating PageMethods, UpdatePanels to jQuery as hinted in lincolnk's reply. But the thing which finally worked was removing the Autopostback attribute from the textbox control.
After removing it the OnTextChanged event executed each time any server postback was initiated after changing the text. Thereby, executing both the OnTextChanged method and the other control's method. :)
I can think of a couple general approaches.
Create a web service with your validation routine and manually make the call (jQuery or whatever) when the text changes. Manually update the client display when you get a result.
Convert your gridview column to a templated field. Add a CustomValidator and wrap the textbox and validator in an UpdatePanel. Set the textbox to auto-postback and the UpdatePanel to conditional update so only the one you are using is refreshed.
Option 1 is kind of an end-around the typical asp.net process, and you would still want to validate everything on the server-side when the page is posted back.
Option 2 might have performance issues, since you're hitting the page again every time you do a validation.
I have a formview with an insertion template. In this template there is a drop down list with a number of items I want users to be able to select from. Beside the drop down list there is a button which I am using to add the selected item from the drop down list to a gridview which also exists in the insertion template.
My problem is that when I click the button to add the selected item from the drop down list the selected item, index or value from the drop down list are not available. I am using a OnClick event handler to catch the event from the button click but I suspect there is some kind of refresh of the template going on here which I am not understanding as nothing appears to be accessible from the button event handler. I don't believe a postback is occurring as I have disabled the CausesValidation property for my button.
It seems like you are binding your DDL on postbacks as well. If the ddl data isnt hardcoded and you have the call for your ddl databind function in the Page_Load, you need to call the function like this to ensure it is not bound on postback:
if(!IsPostBack)
{
BindDDL();
}
Otherwise we need more information to help you and please post your code.
If you are clicking an asp:Button with an OnClick eevent attached to it then you are posting back to the server, no matter whether or not CausesValidation is true or not.
Are you binding data to the DropDownList? If so and you are rebinding it on postback then you'll not have the selected item you're expecting.
Can you paste us the code here?
I would have to see the code, but it sounds like you are getting a rebind prior to pulling the chosen items. One way to examine this is to add a watcha nd then make sure you code in the various ASP.NET events and then watch.
Without seeing what you are doing in code, I cannot tell if this is a drag and drop anomoly or something you have coded in. But the symptoms you are describing fits the typcial bind in Page_Load scenario, which is what jmein was aiming at.
So it turns it out it was all my fault. The formview control I have is contained in a panel which did not have view state enabled. This was preventing the drop down list from remembering the item I had selected it seems.
Thanks all for your comments and suggestions.