I'm developing a dynamic data web app. On list.aspx page it has GridViewPager control for paging, it option in drop down list as 10,20,.. rows in a page like that, but it does not an option show all rows in a page.
How I can add "All" option in it?
I assume you are referring to a GridView and the automatic paging functionality included. If not please clarify. However, if this is the case then the default paging options do not include a show all. You can roll your own, I would start here: http://msdn.microsoft.com/en-us/library/5aw1xfh3.asp
In content folder Dynamic Data site lies the code for GridViewPager control.
What I have done is I added "All" option in dropdown list with values 0, and in the code behind file in function DropDownListPageSize_SelectedIndexChanged I check if selected value is 0 then set AllowPaging = false else true.
protected void DropDownListPageSize_SelectedIndexChanged(object sender, EventArgs e)
{
if (_gridView == null)
{
return;
}
DropDownList dropdownlistpagersize = (DropDownList)sender;
int sz=Convert.ToInt32(dropdownlistpagersize.SelectedValue);
//_gridView.PageSize = Convert.ToInt32(dropdownlistpagersize.SelectedValue);
if (sz<=0)
{
_gridView.AllowPaging = false;
//_gridView.DataBind();
//return;
}
else
{
_gridView.AllowPaging = true;
_gridView.PageSize = sz;
_gridView.AllowPaging = true;
}
int pageindex = _gridView.PageIndex;
_gridView.DataBind();
if (_gridView.PageIndex != pageindex)
{
//if page index changed it means the previous page was not valid and was adjusted. Rebind to fill control with adjusted page
_gridView.DataBind();
}
}
You will have to implement your own pager and attached it to the Gridview. Default pager will not give you this option. May be this link could help you. http://www.codeproject.com/KB/grid/GridView_pager.aspx
Related
I've written a lengthy asp.net webform that has about 44 controls. And this data gets saved into a DB. My problem is, after form submit, I would like to clear ALL the data in the ViewState and the webform controls' contents should be cleared. How is it possible without manually(and tediously) clear each control?
ViewState.Clear() does not work
Page.EnableViewState = false does not work.
If you are staying on the same page, clearing it on the client-side or from the code-behind on the postback would be slightly preferable to a redirect, as you are saving a trip to the server, although it will take more work.
Also, Response.Redirect(url) throws a ThreadAbortionException, which has a negative effect on performance, so if you want to go the redirect route, consider Response.Redirect(url, false).
Client-side option: (the easy way)
<script>
$(':input').each(function () {
switch (this.type) {
case 'password':
case 'text':
case 'select-multiple':
case 'select-one':
case 'textarea':
$(this).val('');
break;
case 'checkbox':
case 'radio':
this.checked = false;
break;
}
});
</script>
Code pilfered from this post.
Server-side option:
You could loop through all the controls to clear them out. At the end of the function that processes your form, add:
ClearForm(Page.Form.Controls);
The function:
public void ClearForm(ControlCollection controls)
{
foreach (Control c in controls)
{
if (c.GetType() == typeof(System.Web.UI.WebControls.TextBox))
{
System.Web.UI.WebControls.TextBox t = (System.Web.UI.WebControls.TextBox)c;
t.Text = String.Empty;
}
//... test for other controls in your forms DDL, checkboxes, etc.
if (c.Controls.Count > 0) ClearForm(c.Controls);
}
}
Looping through Controls and child controls is something that comes up a lot, so you could write an extension method to do this. Something along the lines of what I did in this post (but instead a function that instead returns a collection of all the Controls). I have an extension method in my project that does this, called GetAllChildren(), so the same code above would be executed like this:
foreach (Control i in Page.Form.GetAllChildren())
{
if (i.GetType() == typeof(System.Web.UI.WebControls.TextBox))
{
System.Web.UI.WebControls.TextBox t = (System.Web.UI.WebControls.TextBox)i;
t.Text = String.Empty;
}
// check other types
}
after insertion is completed, just use Response.Redirect to reload the same page from scratch.
for example Page.Response.Redirect(Page.Request.RawUrl)
Try this
public static void ClearFields(ControlCollection pageControls)
{
foreach (Control contl in pageControls)
{
string strCntName = (contl.GetType()).Name;
switch (strCntName)
{
case "TextBox":
TextBox tbSource = (TextBox)contl;
tbSource.Text = "";
break;
case "RadioButtonList":
RadioButtonList rblSource = (RadioButtonList)contl;
rblSource.SelectedIndex = -1;
break;
case "DropDownList":
DropDownList ddlSource = (DropDownList)contl;
ddlSource.SelectedIndex = -1;
break;
case "ListBox":
ListBox lbsource = (ListBox)contl;
lbsource.SelectedIndex = -1;
break;
}
ClearFields(contl.Controls);
}
}
protected void btn_cancel_Click(object sender, EventArgs e)
{
ClearFields(Form.Controls);
}
Sorry, I cannot add a comment for Omkar Hendre answer due to my low reputation.
The code are good and for my problem, I need to put Page before the Form.Controls.
ClearFields(Page.Form.Controls);
By the way, thank you very much! :)
I suggest that you don't play with ViewState. It is meant to properly match the state of the controls.
Instead, just change the state of the controls, either by redirecting, or by clearing the controls explicitly.
I am trying to get this sample to work
Get Selected Row (on server)
but for me the SelectedRow property is always empty.
The only difference being that I am using the Page_load event to populate my grid.
When i press a button on my form, it does a postback, and repopulates the grid losing the row selection.
sample code:
if (!Page.IsPostBack )
{
UserBusinessObject userBO = new UserBusinessObject();
GRDUsers.DataSource = userBO.GetUsersbyProfileID(SessionFacade.Id);
GRDUsers.DataBind();
}
protected void btnEdit_Click(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(GRDUsers.SelectedRow))
{
lblError.Visible = true;
}
else
{
Response.Redirect(string.Format("~/Manage/EditUserRoles.aspx?username={0}",GRDUsers.SelectedRow));
}
}
I Have also noticed that on button click, my Page Load fires twice (1st time postback is true, 2nd time it is false) According to jqgrid posts this is intentional. but i think this might be causing my grief.
You always must set the DataSource because is not saved on ViewState or anywhere else. So the code must be as:
UserBusinessObject userBO = new UserBusinessObject();
GRDUsers.DataSource = userBO.GetUsersbyProfileID(SessionFacade.Id);
if (!Page.IsPostBack )
{
GRDUsers.DataBind();
}
working with the guys at JQGrid, we have resolved the problem. It is a bug in their grid that has been fixed in v4.5.0.0
see here for details
i can confirm that this fixes the bug, and all is right with the world again
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
linking to a radio button selection, asp.net c#
i have a page with a textarea and radio buttons. the text area is populated with data based on the radio button selection. i want the radio button selection to appear in the url so that a user can link to the radio button selection.
i'm hoping that all i need to do i modify my querystring to include radio button value. here's the data caputered by fidler when i make a radio button selection.
__EVENTTARGET ctl00$MainContent$RadioButtonList1$6
__EVENTARGUMENT
__LASTFOCUS
__VIEWSTATE /+++PC9wPg0KPHA+/....
__EVENTVALIDATION /wEWCwKY7d6oAQLh8vmTCALk7M7lDQK+6NunDwK/6OenDwK86OenDwK86OunDwK86POnDwK96NenDwK96NunDwKxh73KA3Q+PMuKU/JUCKsF1aiY2DNLu7/pFFni/Qtz+7FXy35g
ctl00$MainContent$RadioButtonList1 41
i'm hoping my url simply needs to look something like this to point to the radio button value but and all i need is the appropriate syntax:
http://www.test.com/test.aspx?ctl00$MainContent$RadioButtonList1$41
---code behind ---
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
RadioButtonList1.SelectedIndex = 0;
RadioButtonList1.DataBind();
}
else
{
string strRedirect;
strRedirect = "frm_Articles.aspx?Article_PK=" + RadioButtonList1.SelectedValue.ToString();
Response.Redirect(strRedirect);
}
}
protected void SqlDataSource2_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
//
}
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
try{
e.Command.Parameters["#URL_FK"].Value = Session["URL_PK"];
}
catch (Exception ex)
{
}
}
}
One thing to note is that you have misconstructed your url. It should be:
http://www.test.com/test.aspx?ctl00$MainContent$RadioButtonList1=41
(note the third to last character is an =.
Apart from that it would depend on how your page works in terms of picking up the selected radio button. My advice would be to just try it and see what happens. However, I would suspect that if you are doing it based on an event firing on page load (ie some kind of postback behaviour) then it won't work.
If this is the case then you will just want to do something on page load to check if that value exists in the url and set it before you load up the text. If you end up going down this route you might want to consider using a more well defined and user friendly querystring parameter. In particular the id you have there is built based on its position in the control hierarchy. If you were to redesign your HTML structure the ID might well change.
Additionally...
With the code you've provided where you set the selectedindex of your radiobutton list you need to find out what value you need before you set it and set it to 0 if you have no better value (though it looks like you are setting to 0 before binding whcih seems superfluous). Something like this may work (not compiled or tested so apologies for typos - it is designed to give you the general idea more than to be finalised code).
if (Page.IsPostBack == false)
{
RadioButtonList1.DataBind();
//Check if you have a value to set.
if (Request.Querystring(RadioButtonList1.ClientID)!=null)
{
//Get the value
string setValue = Request.Querystring(RadioButtonList1.ClientID)
//Find the right radio option to select
foreach(ListItem item in RadioButtonList1.Items)
{
if (item.Value == setValue)
{
item.Selected = true;
break;
}
}
}
}
I assume it will default to the selected index being 0 if nothing is set as selected otherwise.
Anyway, this code is meant as a starting point to work from. There may be other ways to do it and some may even be better.
I am using JScript in ASP.NET 2005. I have a page with a checkbox and a dropdown list. When the checkbox is checked, the dropdown is to be disabled. During Postback of the page, this behavior should be retained. I am using a javascript function as follows
if((chkOwner[1].checked==true))
{
document.getElementById(ddlClientID).disabled=true;
document.getElementById(ddlClientID).className = "form-disabled";
document.getElementById(ddlClientID).selectedIndex = 0;
}
else
{
document.getElementById(ddlClientID).disabled=false;
document.getElementById(ddlClientID).className = "form-input";
document.getElementById(ddlClientID).selectedIndex = 0;
}
This works, almost. However, the dropdown selection is not retained after postback (when checkbox is not selected). [It goes to zero index's value]
Apprently the solution is to remove the last line, i.e, in the else part, remove the selectedIndex =0 .
But, when I do that the disabling of dropdown (when check box is checked) is not working after post back.
Could you please help me on this?
More Info: I am using ClientScript.RegisterStartupScript for checking this at each page load.
Thanks
Lijo
--CODE-----
Following is what I am trying. (This is a sample application I created just now. It does not work. This can only show what I am trying to achieve.)
function ManageInputsFor()
{
if((document.getElementById(chbx).checked==true))
{
document.getElementById(ddlClientID).disabled=true;
document.getElementById(ddlClientID).className = "form-disabled";
document.getElementById(ddlClientID).selectedIndex = 0;
}
else
{
document.getElementById(ddlClientID).disabled=false;
document.getElementById(ddlClientID).className = "form-input";
document.getElementById(ddlClientID).selectedIndex = 0;
}
}
protected void Page_Load(object sender, EventArgs e)
{
ClientScript.RegisterClientScriptBlock(Page.GetType(), "chbx", #"<script language=""javascript"" type=""text/javascript"">var chbx ='" + CheckBox1.ClientID + "'</script>");
ClientScript.RegisterClientScriptBlock(Page.GetType(), "ddl", #"<script language=""javascript"" type=""text/javascript"">var ddlClientID ='" + DropDownList1.ClientID + "'</script>");
ClientScript.RegisterStartupScript(Page.GetType(), "onLoadCallForManageInputs", #"<script language=""javascript"" type=""text/javascript"">ManageInputsFor();</script>");
CheckBox1.Attributes.Add("onclick", "ManageInputsFor();");
}
you'll have to do a server side evaluation of the checkbox during the pastback, and set the dropdownlist enabled or not. the disabled attribute of the drop down isn't sent to the server during the post operation (neither is the value of the control when it's disabled,btw).
edit: to retain the value in the drop down across a postback, you can copy the selected value to another control which will be posted back, and then assign the value back to the drop down during server processing.
Basically, your problem is happening bc when you use javascript to change the state of an ASP control, that stat is not posted back to the server. You have to figure out a way to send it. Using a hidden control, or adding a parameter to the querystring are two such methods.
is there some way to force the gridview's pager to show up, even when there is only one page of data on screen?
I'm building a gridview-based control with a custom pager (w/dropdown for pagesize) and everything is working fine, except when user selects pagesize that is larger than the current row count of the grid. At that point the pager disappears. I've been googling this and i think that i should be doing something in override OnRowCreated...
Custom pager is added by overriding InitializePager. I'll be glad to provide more information if required!
greets,
J.Arola
Ok, that wasn't too hard :-)
Based on my initial testing the following did the trick:
GridViewRow pagerRow = (GridViewRow) this.BottomPagerRow;
if(pagerRow != null && pagerRow.Visible == false)
pagerRow.Visible = true;
I just added that to overridden OnPreRender, and lo, pager is visible, even when there is just one page page of data shown. Got to do some additional testing before I can be sure, though. Seems to simple to me.
The above will work
But this might be helpful also
GridView.BottomPagerRow.Visible=true
protected void GridView_PreRender(object sender, EventArgs e)
{
GridView gv = (GridView)sender;
GridViewRow pagerRow = (GridViewRow)gv.BottomPagerRow;
if (pagerRow != null && pagerRow.Visible == false)
pagerRow.Visible = true;
}
GridView.BottomPagerRow.Visible=true works like a charm