I'm creating an ASP.NET/C# page where a user enters an ID at the top of the page, and when they press Submit, the page posts back and displays the data. Right now, the data fields are displayed at all times - even when the page loads and no search has taken place. This is not ideal as when they navigate a Menu with a MultiView I have made, some errors will come up. Plus it's obviously not good practise.
Is there a way I can hide all these elements (they are DetailsViews and GridViews, plus the MultiView and Menu) if the SQLDataSource is blank? So if the search returned nor esults, or no query has been executed yet.
Thanks
You could surround these elements is a placeholder control and then set the placeholders visibility depending on whether there are result to display.
I haven't used the SqlDataSource object much before but to meet you requirements I would suggest checking to see if the page has been posted back in the PageLoad method and if not hiding the data controls. Then to handle the case of no results being returned adding a method to the SqlDataSource.Selected event:
ASPX:
<asp:PlaceHolder ID="myPlaceholder" runat="server">
....Data controlds
</asp:PlaceHolder>
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
if(!isPostBack)
{
myPlaceholder.Visible = false;
}
}
protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
myPlaceholder.Visible = e.AffectedRows > 0;
}
If GridView1.Rows.Count > 0 // then do stuff
DataView dv = (DataView)SqlDSourse.Select(DataSourceSelectArguments.Empty);
if(dv.count>0)
{
...
}
Related
I have a page that has many components like:
text boxes.
labels.
images.
forms.
and all of these are depending on the url parameter. Thus, in the page load, i got sure that the parameter is existed and I validate its value according to my business rules.
I want if that url is not existed, to show a message error.
I tried this:
Response.Write("Please don't change anything in the URL");
It works good, but my problem: is that the other elements in the page are showing as well, i need not to show the other elements.
is that possible? or I should go to each element and make the visibility property as false?
In Page_Load or Page_Init you can check whether some condition is met regarding the query string. Calling Response.End(); will stop the execution of the page events. So in summary:
protected void Page_Init (object sender, EventArgs e)
{
if(!IsQueryStringValid(Request.QueryString))
{
Response.Write("Please don't change anything in the URL");
Response.End();
// OR
// Response.Redirect("~/QueryStringModifiedError.aspx");
}
}
I'm trying to set the pagination for a GridView that has a LINQ generated list, but though the function I wrote to handle OnPageIndexChanging attribute in the GridView shows the records as planned, the footer with the page numbers dissapears when I navigate to any page. I'd appreciate some orientation on this as I'm new to C# and asp and I don't have a clue as to why this is happening. I realize there are lots of ways of doing this, some involving new GridViewRow() and even generating an EmptyDataTemplate with custom html, but I don't quite understand what those functions do. I'd appreciate if someone commented on my code, what I'm missing.
I have set the GridView with AllowPaging true of course, and my Pager Settings are: mode NumericFirstLast, and Visible=true. When I debug the code the footer visibility shows it's true yet the page numbers dissapear.
My asp is like this (not all of it I only include the definition of the GridView):
<asp:GridView ID="GridCol" runat="server" DataKeyNames="identMovimiento"
AllowPaging="True" ShowFooter="True" OnDataBound="GridCol_DataBound" OnPageIndexChanging="PaginarCol">
And the PaginarCol function is like so (I roughly repeat here some code I use in the Page_Load function, because I want to populate the GridView again with the same source:
protected void PaginarCol(object sender, GridViewPageEventArgs e)
{
GridCol.PageIndex = e.NewPageIndex;
int numResultados, inicioResultados;
matriculas identificacion = new obtenerMatricula().getMatriculaDatos(User.Identity.Name);
int idusuario = identificacion.matriculaId;
obtenerAdeudos Lista = new obtenerAdeudos();
List<Adeudos> ListaAdeudos = Lista.getAdeudosCol(idusuario);
int maxResultados = ListaAdeudos.Count();
numResultados = 10; inicioResultados = GridCol.PageIndex*10;
List<Adeudos> filasAdeudos = ListaAdeudos.Skip(inicioResultados).Take(numResultados).ToList();
GridCol.DataSource = filasAdeudos;
GridCol.DataBind();
GridCol.BottomPagerRow.Visible = true;
}
Your page numbers are disappearing because in the page index changing event (PaginarCol()), you are binding exactly one page of data, because of the Skip() and Take() calls. Since the grid is bound with only one page of data, there is no need for page numbers, thus they disappear.
My guess, since you did not post the Page_Load code, is that you are binding the whole list initially and not using Skip() and Take() to only get the one page of data. This would actually cause the grid to realize that there are pages of data instead of just one, thus the page numbers appear when the grid is first loaded.
If you want your existing code to actually keep showing the page numbers, then remove this entire line:
List<Adeudos> filasAdeudos =
ListaAdeudos.Skip(inicioResultados).Take(numResultados).ToList();
Now instead just bind directly to ListaAdeudos, like this:
GridCol.DataSource = ListaAdeudos;
Since your PaginarCol() method is updating the grid's page index value, via this line:
GridCol.PageIndex = e.NewPageIndex;
Rebinding the grid will take care of the correct page number being selected and all of the page numbers continuing to be displayed.
I am needing to use the same form for displaying multiple things and I realize that when I add links like:
A
it has the same form and web address, but with a #A at the end of the address.I thought I could use this for displaying multiple things on the same form. My idea is to have C# code in the page_load method to detect what the web address is and use a conatins method for the url string and detect if there is #A to change the content of the form. Here is an example:
C# code:
protected void Page_Load(object sender, EventArgs e)
{
string url = HttpContext.Current.Request.Url.AbsoluteUri;
if(url.Contains("#A"))
{
div1.Visible = false; //content 1
div2.visible = true; //content 2
}
}
asp.net code:
A
<div ID="div1" runat="server">
content 1
</div>
<div ID="div2" runat="server">
content 2
</div>
I have tried to put the Page_Load method in a script tag, but still didn't work. I guess since the url is different the cs code is not valid? I know it goes through the page_load method once, before I click on the link. Also I do use a method that gives me the controls of div1 and div2, so that is not the problem. I thank everyone in advance for your help! Also if my way is not the way to do the job then please tell me any way possible to achieve what I am trying to do.
edit: I can't use a button to replace a link... maybe a asp:hyperlink?
That's an HTML hyperlink you're using and it won't cause a postback thus page_load will never get called when you click it.
I would suggest if you want to show an hide divs that you use client side JavaScript. Alternatively you could (for example) use an asp.net button control which will cause a postback.
I would suggest scrapping the anchor with an href approach in favor of this:
Use the ASP.NET server controls, along with their click event handlers to manage the visibility of controls on your page.
In your Page_Load, make it so the page has an initial state of showing controls, like this:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
div1.Visible = false; //content 1
div2.visible = true; //content 2
}
}
Now instead of an anchor tag, you can use an ASP.NET Button or LinkButton to cause a postback to the server, like this:
<asp:Button id="Button1"
Text="Click here to change page to B"
OnClick="Button1_Click"
runat="server"/>
Now you have the event handler code which would change the visibility of controls, like this:
protected void Button1_Click(Object sender, EventArgs e)
{
div1.Visible = true; //content 1
div2.visible = false; //content 2
}
I wrote a ASP.NET Application and it run in IIS7 of a Server. If I open this webform in my Browser and show me the Sitecode I see this...
I have many Controls how Buttons,Labels,TextBoxes and a ListView. I try to deactivate ViewState in the web.config but if I deactivate this my Application don't run correctly. What can I do?
Deactivate only the controls that not need the viewstate.
To do that you need to understand what the viewstate is.
Viewstate is where the page save and remember the values of the controls to have them after a post back. Remember that, the viewstate is used after a post back.
So actually you have two times the same data, but only the viewstate is post back the previous data and code behind can be use that data.
So the main question is, what controls do you need to be remember what you have fill them in, or what controls need to remeber the previous state of them.
Lets see a simple Literal with EnableViewState on and off.
ViewState ON
<asp:Literal runat="server" EnableViewState="true" ID="txtLiterar">
Now if you place a text on this literal the text is also saved on viewstate and on code behind you can do that.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txtLiterar.Text = "Hello There";
}
}
So after the post back the Literal still have its content, and you can avoid to fill it again, because the viewstate have it and automatically fills it again.
ViewState OFF
<asp:Literal runat="server" EnableViewState="false" ID="txtLiterar">
Now if you place a text on this literal the text is not saved on view state and on code behind you add it as.
protected void Page_Load(object sender, EventArgs e)
{
txtLiterar.Text = "Hello There";
}
So the different is that you need to always fill that control with data on every post.
Where the viewstate is needed most.
The most needed part of the viewstate is when you fill a dropdown list. There you have a databind and code behind need to remember the values to place on the SelectValue the correct one.
Its also needed on GridView and other controls like that because is keep the previous page and other information's when you paging your data.
So you can close on most of your controls the viewstate - on that controls that you can fill them again on every post back, and on that controls that not need to remeber the previous state.
More to read:
How to optimize class for viewstate
Determine size of ASP.NET page's viewstate before serving page
Limiting view state information on AJAX calls
I have a a generic control called dropdownlist.ascx that populates it's ListItems based on a XML document and a property that I pass to this control.
This control is used multiple times on the same aspx page. I have no problem casting this control as a DropDownList control in the Page_Load event of the aspx page, however when I want to set the SelectedValues of this control on the Page_Load event of the aspx page it doesn't work as the Items.Count value is 0.
I assume there are some Page Lifecyle issues going on here.
Control on page.aspx
<triangle:DDLResponse ID="ddlHeight" runat="server" CssClass="dropdownlist ddlregister" responseId="height" mode="dropdownlist" />
Codebehind on page.aspx
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
UserProfile profile = controls_session.profile;
DropDownList _ddlHeight = (DropDownList)ddlHeight.FindControl("dropdownlist");
_ddlHeight.SelectedValue = profile.Height;
}
}
The List Items of ddlHeight render without issue.
Anyone have any idea or solution to this?
Thanks,
Try to set the SelectedValue in Page_PreRender event. It will work.