GridView Custom Paging - First, Last, Next, Previous,Numeric - asp.net

I am using a sql store procedure for custom paging.
Now i want to make a custom pager for gridview.
I have searched on google and find various articles about it.
But i am not getting which i want.
please help me for it if you have any idea about it.
Thanks,
Rajbir

I think you looking for something like this
<ItemTemplate>
<asp:LinkButton ID="lnkPage" runat="server" Text = '<%#Eval("Text") %>' CommandArgument = '<%# Eval("Value") %>' Enabled = '<%# Eval("Enabled") %>' OnClick = "Page_Changed"></asp:LinkButton>
</ItemTemplate>
You will find more form Custom Paging in ASP.Net GridView using SQL Server Stored Procedure

there is nothing to do with Stored Procedure to use Paging in Gridview. Only you need to do is to bind your GridView on OnPageIndexChanging event.
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
}
Please visit this link for example: ASP.NET GridView with Custom Paging UI

Related

SelectedIndexChanged event of gridview fails to fire randomly

Hi I have a Gridview SelectedIndexChanged event. it fires on click of any row in that grid. But sometime (once in 10 tries) it doesnt fire. i have some code inside AgeGrid_SelectedIndexChanged which set the session variable. when it fails not able to get proper value.
Any Idea why it fails randomly?
i have not set EnableEventValidation="false" property for that page. And using visual studio 2005.
<Asp:GridView ID="agendaGrid" runat="server" Width="96%" GridLines="Both" EnableViewState="true" HeaderStyle-CssClass="GridViewHeader "
OnSorting = "AgeGrid_Sorting" CssClass="GridView"
AutoGenerateColumns="false" OnSelectedIndexChanged="AgeGrid_SelectedIndexChanged"
AllowPaging="true" PageSize="10" ItemTemplateType="PopUpMenu"
ShowTemplateFields="true" EnableOnClick="true" RowStyle-Font-Size="13px" RowStyle-VerticalAlign="middle"
EnablePopUpClick="true" OnRowDataBound="AgeGrid_RowDataBound" AllowSorting ="true"
>
</Asp:GridView>
Any Suggestions are welcome.
is there any thing i need to add or delete from page property or Gridview property?
protected void AgeGrid_SelectedIndexChanged(object sender, GridViewPageEventArgs e)
{
agendaGrid.PageIndex = e.NewPageIndex;
BindGrid();
}
may problem in binding data after paging, not eror in pagination.
May its helps you.

Getting an Instance of Dropdown list when clicking update on GridView in Asp.Net

I have a GridView with more than 30 columns. Most are plain controls but for some I have added a template control (DropDownList, Calendar and CheckBox control). Here is the aspx code for the control in question
<asp:TemplateField HeaderText="Field1 Caption" SortExpression="Field1">
<ItemTemplate>
<asp:Label ID="lblConstructionArea" runat="server" Text='<%# Eval("Field1") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlField1" EnableViewState="true" runat="server"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
I wanted a dropdown to be shown on the column when a user clicked on Edit. So I add this code (and the above EditItemTemplate)
protected void gvData_RowEditing(object sender, GridViewEditEventArgs e)
{
string fieldOne = CommonUtils.ExtractControlValue(e,"lblField1",gvData);
gvData.SelectedIndex = e.NewEditIndex;
gvData.EditIndex = e.NewEditIndex;
gvData.DataBind();
BindGridDropDownData(e, CommonUtils.GetConstructionAreas() ,"ddlConstructionArea", constructionArea, "Field1", fieldOne);
}
In the above code I am getting the current available and passing it to another method so that when the dropdown is displayed the selected index can be shown accurately. After this I do a change on the dropdownlist and click on the "Update button" on the GridView and the following event is triggered
protected void gvData_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int rowEditIndex = e.RowIndex;
GridViewRow gRow = gvData.Rows[rowEditIndex];
DropDownList ddlConstructionArea = (DropDownList) gvData.Rows[rowEditIndex].FindControl("ddlConstructionArea"); //This does not work
ddlConstructionArea = (DropDownList)gRow.FindControl("ddlConstructionArea");//This does not work
ddlConstructionArea = (DropDownList)gvData.Rows[rowEditIndex].Cells[7].FindControl("ddlConstructionArea");//this does not work either
gvData.EditIndex = -1;//this works and the text boxes disappear
gvData.DataBind();//this works and the old data shows up on the gridview
}
I am curious as to how to do an update on a Grid where I have the binding is runtime.
Actually in the markup, you have given ID of dropdownlist as ddlField1 and in codebehind, you are reffering it as ddlConstructionArea. Is this is what causing the update not to function?
The problem was with the way the grid was being bound. I had written code to refresh the grid on page load and anytime I clicked on the Edit button the page was being refreshed and the grid binding was getting triggered instead of the updating event. I removed the code refreshing the grid on page load and put it in the places where it is needed and the order of events were getting triggered the way I would want it to and the update worked perfectly without a problem.

How to force a value in asp.net TextBox and Bind at the same time?

I'm currently working on a phone input control with 3 asp.net TextBoxes (international, regional and actual number) that are linked to a datasource with a 2-way databind. I was asked to force a "+" in the international textbox (when editing or creating).
The idea is similar to this:
<asp:TextBox ID="txtInternational" runat="server" Text='+<%# Bind("telephone_international")%>' />
Which does not work in this case.
Do anyone have and idea, should I do it using RegEx?
Thanks
PatH
If you used textBoxes under GridView, ListView, Repeater or DataList controls then register a ItemDataBound event. And in code behind write this (Note: following event is for DataList control): -
protected void dlSample_ItemDataBound(object sender, DataListItemEventArgs e)
{
TextBox tb = (TextBox)e.Item.FindControl("txtInternational");
tb.Text = "+" + tb.Text;
}
<asp:TextBox ID="txtInternational" runat="server"
Text='<%# string.IsNullOrEmpty(Eval("telephone_international")) ? "" : "+" + Eval("telephone_international")%>' />

Add new row to database table from Datagrid

I am trying to update a database table from my datagrid using an event handler and ItemCommand. I manage to call the routine and everything is working fine except the text that is inserted into my database is empty. I managed to track this back to the text not being passed from my datagrids footer to the sql parameters. I tried using a string first and then passing that to the parameters but they were also empty. I am accessing the control using the following line.
sqlcmd.Parameters.Add("#GoodsDesc", SqlDbType.VarChar).Value = CType(e.Item.FindControl("txtGoodsDesc"), TextBox).Text
The control itself is defined using
<asp:TemplateColumn HeaderText="Goods Descriptions">
<ItemTemplate>
<asp:Label runat="server" ID="lblGoodsDesc" Text='<%# Eval("GoodsDesc") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtGoodsDesc" runat="server" TextMode="MultiLine" Rows="3"></asp:TextBox>
</FooterTemplate>
</asp:TemplateColumn>
Am I missing something here? It's like the text in the footer isnt being tied to the control before I call it.
HI Ryan, we will need more code then just that. Where are you .Add +ing this parameter in addition where is e.Item.FindControl what event is it?
You need to check if you are on the footer control:
protected void dg_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
if (dg.EditItemIndex != -1)
{
((TextBox)e.Item.FindControl("txtGoodsDesc")).Text
}
}
}
Or in vb.net
if (e.Item.ItemType = ListItemType.Footer) then
Dim s as String=String.Empty
s=CType(e.Item.FindControl("txtGoodsDesc"), TextBox).Text
end if
So essentially I discovered that the issue that was occurring was that VB.Net was calling Page_Load before the event itself was being called, and in the Page_Load method I was calling the method that filled the datagrid, which cleared the textboxes in the footer before the values in them were being read.
To stop this I placed a condition around the call to the method in Page_Load
If Not IsPostBack Then
FillDataGrid()
End If
Then I called the FillDataGrid() function as the very last step in the handler, meaning that the data was read in and then the datagrid was bound to the new values.

Image upload via gridview question

I have a gridview that displays data from a database. The datbase stores the image filename (string) among other things for an item.
I have the fileupload control showing in the EDIT view, and that uploads the file just fine.
The problem is, I want to update the image filename in the database and I am not sure how to get the data to the textbox control that the gridview uses to UPDATE the database. The textbox control i have set the visibility to hidden. here is the aspx code:
<asp:TemplateField HeaderText="Image" SortExpression="Image">
<EditItemTemplate>
<asp:TextBox ID="txtImage" runat="server" Text='<%# Bind("Image") %>' Visible="False" OnTextChanged="txtImage_TextChanged"></asp:TextBox>
<asp:FileUpload ID="FileUpload1" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" Width="50px" AlternateText='<%# Eval("Image") %>' ImageUrl='<%# "images/hardware/" + Eval("Image") %>' />
</ItemTemplate>
</asp:TemplateField>
And here is the function that stores the file onto the server, and places the filename into a variable:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
FileUpload fileUpload = row.Cells[0].FindControl("FileUpload1") as FileUpload;
if ( fileUpload != null && fileUpload.HasFile)
{
strFileName = fileUpload.FileName;
fileUpload.SaveAs(Server.MapPath("images/hardware/" + strFileName));
TextBox txtImage = row.Cells[0].FindControl("txtImage") as TextBox;
txtImage.Text = strFileName;
}
}
So where do I go from here? I think the update has occured already or something? Because the "txtImage.Text" does not update the database... Am I out of order here or something? I can manipulate values in the textbox in this function before it gets saved to the DB right? Thanks for your help on this one.
If you handle updating the database in the code behind you should be able to get the file name like you did in GridView1_RowUpdating. Just find the row in the grid that is in edit mode. Each row in the rows collection on the grid has a RowState property...your looking for DataControlRowState.Edit
I think I have a solution, but would like input into whether this is a good way of coding it.
I have this setup as the "Updating" event for the dataset the gridview uses:
protected void SetRecords(object sender, SqlDataSourceCommandEventArgs e)
{
if (strFileName != "")
{
e.Command.Parameters["#Image"].Value = strFileName;
}
}
Is this ok? Everything seems to work just fine... but is this a good way to solve the problem? I would like to code properly and not start bad coding practices right from the start (I am an accomplished classic ASP developer moving to C# and ASP.NET)
Thanks all!

Resources