GridView paging allowed but disabled, possible? - asp.net

For a GridView, is there any way I can have AllowPaging="true" but have the pagination links disabled (still visible but not clickable)?
(It's for when the user decides to edit the GridView. In edit mode, Labels inside cells become TextBoxes.)
I've tried Enabled="false" but this disables everything, including the TextBoxes.
I suppose I could handle paging on the server side but I'd rather just disable the pagination links if it's possible.
Any ideas appreciated!

I would hide Pager on RowEditing and show it again on Cancel or Update:
void CustomerGridView_RowEditing(Object sender, GridViewEditEventArgs e)
{
// Hide the pager row.
CustomerGridView.PagerSettings.Visible = false;
}
void CustomerGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Cancel" || e.CommandName == "Update")
{
// Show the pager row.
CustomerGridView.PagerSettings.Visible = true;
}
}

Related

Set row height of gridview in webform when in edit mode

I have a gridview that I need to use to edit and update a table in SQL. I have the gridview, the edit, update and cancel functionality all set. My problem is that when the user selects the edit function, the row height drops to a single line. Unfortunately, the cells that need to be updated are comment fields so there can be more than one line of information. What I would like to do is change the edit mode to resize the row height so the user can see the entire cell contents. I have tried the DataGridView.AutoSizeRowsMode but this only applies to a windows form not a webform.
I am using Visual Web Developer, SQL2008R2 and asp.net
In the GridView_RowDataBound you can check the row being edited and set it's height:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex == GridView1.EditIndex)
{
e.Row.Height = 340;
}
}
Assuming that you have something like this in RowEditing:
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGridView();
}

How do I keep an edit button hidden in a formview after using a filterexpression?

I'm trying to have a user/admin scenario for editing a formview. What I have is the Edit buttons in the formview be visible=true by default. Then I use a statement which says if the role is not equal to admin, then set the visibility to false for the edit button. It works like it should, when I click around, do postbacks, etc. the items stay hidden if the user is and admin...except when I use a dropdown list for filtering, which uses a statement like dataSource1.FilterExpression = ("ID=" + ddl1.SelectedValue).
Whether I set the default visibility to true or false for the formview edit linkbutton, when using the FilterExpression, it changes the visibility of what I don't want it to be. It is because the FilterExpression sets the visibility of the control to whatever the default visibility property of the control is set to.
Below is some code as an example of what I mean. Please help!
protected void Page_PreRender(object sender, EventArgs e)
{
string role;
role = "client";
LinkButton editGeneralOverview = (LinkButton)formViewGeneralOverview.FindControl("EditButton");
if (role != "admin"))
{
editGeneralOverview.Visible = false;
}
if (ddlIDFilter.SelectedValue != "-- ALL --")
{
dataSourceGeneralOverview.FilterExpression = ("ID=" + ddlIDFilter.SelectedValue);
}
You could hide it in the FormViews DataBound event:
protected void formViewGeneralOverview_DataBound(Object sender, EventArgs e)
{
LinkButton editGeneralOverview = (LinkButton)formViewGeneralOverview.FindControl("EditButton");
if (role != "admin"))
{
editGeneralOverview.Visible = false;
}
}
This way your visibility setting gets (re)applied after the FilterExpression has already taken effect.
Note: in case you do not know, you can attach this event to the FormView in the markup by setting the OnDataBound property. Like this:
<asp:FormView ID="formViewGeneralOverview"
OnDataBound="formViewGeneralOverview_DataBound"
...whatever other propeties you have >
...templates and junk...
</asp:FormView>

Store the Contents of a Panel Control into a Cookie

I created a page, which allows users the option to show or hide it's content sections via a show/hide button. After clicking the show/hide button, the nested panels/content becomes visible or invisible based on the button selected, then the user may save the page by clicking a save button. Problem - (no errors) but, the page is not saving the users changes into the cookie. The page contains 2 panel controls that are nested in one main Panel control.
//Front End code - The save button
<asp:Button ID="savButton" runat="server" Text="Save" onclick="savButton_Click" />
//psuedo code - The Panels
<asp:Panel ID="pnlSaveContent" runat="server"> //main Panel control
<asp:Panel ID="pnlWeatherAppCtrl" runat="server"> // panel content 1
<div>Weather App Content</div>
</Panel>
<asp:Panel ID="StockAppCtrl" runat="server"> // panel content 2
<div>Stock App Content</div>
</Panel>
</Panel>
//Back-end code:
protected void Page_Load(object sender, EventArgs e)
{
//get the cookie
if ((Request.Cookies["preferences"] != null))
{
pnlSaveContent.ID = Request.Cookies["preferences"]["savePg"];
}
}
//set cookie
protected void savButton_Click(object sender, EventArgs e)
{
Response.Cookies["preferences"]["savePg"] = pnlSaveContent.ID;
Response.Cookies["preferences"].Expires = DateTime.MaxValue;
}
//end code
...the issue: The page is not saving the changes of the main panel control. Could someone please provide some guidance as to what I’m doing wrong?
Don't forget to save the cookie with Response.Cookies.Add:
protected void savButton_Click(object sender, EventArgs e)
{
HttpCookie c = Request.Cookies["preferences"] != null ?
Request.Cookies["preferences"] :
new HttpCookie("preferences");
c.Values["savePg"] = pnlSaveContent.ID;
c.Expires = DateTime.MaxValue;
Response.Cookies.Add(c);
}
As for your comment... I'm not quite sure what you are trying to do, but maybe it is this. This will set the visibility of the panel depending on the value of the cookie (visibility is false if the ID matches the value of cookie).
protected void Page_Load(object sender, EventArgs e)
{
//get the cookie
if ((Request.Cookies["preferences"] != null))
{
pnlSaveContent.Visible = !(pnlSaveContent.ID == Request.Cookies["preferences"]["savePg"]);
}
}

checkbox.checked is always false in a grid view in IE

I have a checkbox inside a GridView. When i click on save button, i check at code behind which check boxes are checked to save the record in dbms. But Checkbox.checked property is always false.
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chbx = (CheckBox)row.FindControl("chk1");
if (chbx.Checked)
{
// ...
}
}
However it works fine in Mozilla but not in IE.
That's very rare that your server side code is working on one browser but not on another, But most common reason of this problem is that you might be binding your GridView in Page_Load without checking IsPostback property,
your code should be like this
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
BindYourGridView();// Bind your grid here only during first page load not every time
}

ASP.Net - Gridview row selection and effects

I'm using the following code in attempt to allow the user to select a gridview row by clicking anywhere on the row (plus mouse over and out) effects. The code doesn't seem to be applied on rowdatabound and I can't break into the event. (It is wired).
The control is in a usercontrol, that lives in a content page, which has a masterpage.
protected void gvOrderTypes_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridView gvOrdTypes = (GridView)sender;
//check the item being bound is actually a DataRow, if it is,
//wire up the required html events and attach the relevant JavaScripts
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onmouseover"] = "javascript:setMouseOverColor(this);";
e.Row.Attributes["onmouseout"] = "javascript:setMouseOutColor(this);";
e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(gvOrdTypes, "Select$" + e.Row.RowIndex);
}
}
Scratch that. The handler was wired to the databound event instead of rowdatabound. Sorry everyone. As a side note, the code works

Resources