I have a gridview in my Master page.I have taken an item template inside it. I have given some text to the controls initially.
In the design view of the content page gridview is visible but while running the page it's not showing gridview.
when i deleted gridview and put some simple text in place of it ,it's working .
i tried setting z- index and some peculiar things but nothing seems to work.Please help me out
There may be number of causes - you might not bind it or datasource is empty or its visibility state is hidden or you have placed GridView inside the ContantPlaceHolder in master page.
Confirm the below cases to show the gridview, listed below:
a. Must have to put "DataSource" and then call DataBind() method, like
gridViewToShow.DataSource = dataSourceName; // may be table or custom data source
gridViewToShow.DataBind();
b. Data must exists in the "DataSource"
c. Locate data field( <%# Eval("DataFieldName") %> ) to Gridview column, like in aspx code
c. Make it visible (or don't make invisible, by default it is true)
d. If you use CssClass, then make sure there has "display:block;" or has no "display:none;"
Hope these would be very helpful.
Try setting the EmptyDataText to something and see if it shows the text. If it does, it means it is not DataBound to anything, or the DataSource has no rows. Good luck!
Related
I have an aspx page with the following code:
<asp:Table ID="JsonContent" runat="server">
</asp:Table>
In my behind code at Page_Load i add Items to Table.
Table consist two rows:
Row num 0 is Cell that contain TextBox
Row num 1 is Cell that Contain TextBox
Additionally i have a button save, when the user click the button i need to go over all table row and write the data at Text Prop to File.
The problem is that when i click the Button Save the event page_load raise and all text box control is not already exist so i can no write the edited data to file.
I read about similar post with this issue, but could not get a solution.
Thank!
First of all, you should learn about asp.net pages web cycle : https://msdn.microsoft.com/en-us/library/ms178472.aspx
And i would say that #TimSchmelter has the perfect solution for you in the comments :
"You should (re-)create all dynamically added controls in Page_Init in every postback because all objects are disposed when html is sent to the client. But even if you recreate them there they will persist the user-input if you assign the same ID as before"
This means you have to use the function page_init (wich is similar to page_load but not automaticly generated, you have to write it manualy)
I did this once too, and I confime that Tim's solution will work.
Just make sure that your page_init() fucntion is reached.
I'm facing a problem with one of my pages. I have a FormView control on the page that I use to enter new rows into a database table, and a GridView control on the same page to update/delete rows from the same database table.
The FormView control has validators on it to validate any input being thrown at the database, and it seems to interfere with the GridView when I try to edit a row. When I try to save the edit, the validation control on the FormView gets fired and an error comes up because the textbox input is blank, so the GridView cannot save the modified table data.
Perhaps a visible example will help:
I had an idea where I encase these controls in different forms, hoping that the submit of one form won't fire anything in the other but then I got an error saying I can't have more than one form on the page with runat="server", which as far as I can tell is required.
How can I get around this?
Thanks.
You can only have one Form.
Use different ValidationGroups for your FormView and GridView instead.
http://msdn.microsoft.com/en-us/library/ms227424(v=vs.100).aspx
Yes, this is a known limitation (feature?) of webforms. You can only have 1 form with a runat="server" tag.
In your scenario you can use ValidationGroup names to group validation together so only "like" validation is executed when a control of the same validation is triggered.
What I did is I had two forms coded as postback to the page and parsed out the postback variables in Page.Load myself. Weird? Yes. Works? Yes.
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 have a table similar to:
ID...NAME.....HTMLTEXT
1....Footer....`<b>test</b>`
where each entry has some HTML text and an associated Name and ID.
What I want to do in ASP.net C# is to list each of these entries on a page in such a way that the HTML text should be displayed exactly how it is meant to be (e.g. <b>test</b> should show 'test' in bold) and each entry should be editable. When the Edit button is clicked on an entry, the HTML text should be replaced by a textbox with the text inside it so that you can edit it and save it.
For example:
FOOTER --EditButton--
TEXT
test
Now I am not sure on what is the best way to do this. Should I use a repeater with an ItemTemplate for each entry? If I use a repeater, and an edit button is clicked, how do I know which edit button is clicked and how do I know which textbox to display the text etc?
You could also use a ListView - I tend to use this because its pretty robust. The data bound controls support the <%# Bind("Field") %> statement. You setup your templates in ItemTemplate or EditItemTemplate, and if you add the proper commands to the buttons in the UI, it all wires up the proper events for you (depending on how you data bind). If you don't use a datasource control, you have to manually tap into the ItemEditing, ItemUpdating, etc. events.
Additionally, if you need to, you can get references to the controls using the current item's FindControl("ID") method.
HTH.
Create a custom control (.ascx) and on this control make the line with the view that you wish, and handle the change/save/delete etc.
Then place this control in the repeater and just give him an id to load and edit.
My problem is that all the textbox's my formview are getting cleared when I hit the submit button.
I currently have a page with a small section that has an update panel around it. This small section adds an address to my databse. To the left of this form there is a gridview that is tied into the formview. So if i click on an item in the gridview its contents fill the address section(formview) with the correct data.
When I hit add the data gets validated in the c# code behind, and if all the info is correct the address is inserted. If there is an error the entire form is cleared and the error message(label) is displayed.
I have already done this many times in other pages, but none have had the gridview tied to the formview, and they have all worked. I tried removing the gridview and the form still erases itself.
Is there some reason that .net thinks it should be clearing the form? When in other cases it decides it won't? If so what are these cases, or what general tips should I try to solve this?
in the page_load are you using if(!Page.IsPostback) { ... } so if it's a postback nothing gets re-bound?
is the ViewState enabled?
Yup many hours later I found that a single panel that was wrapped around the section had a EnableViewState="false" added to it. Sad part is that I know I didn't add that because I didn't even know what it was until craig here mentioned it. Visual Studio must have added it sometime.