I have a GridView with checkbox, dropdown and button as templatefields.
On button click I have to update the entire row in the data base.
This is my aspx:
<asp:GridView runat="server" ID="Gdv" AutoGenerateColumns="False" Font-Size="Small" CssClass="grid" BackColor="White" BorderWidth="0px" CellPadding="4" Width="100%" AllowSorting="True" SkinID="GVSalesManager" GridLines="none" AllowPaging="true" PageSize="10" PagerStyle-ForeColor="#0066cc" PagerStyle-CssClass="gvPagerCss" PagerStyle-Font-Underline="true" PagerStyle-Wrap="true"
OnPageIndexChanging="GdvCPRetailerMap_PageIndexChanging" OnRowDataBound="GdvCPRetailerMap_RowDataBound">
<Columns>
<asp:BoundField ReadOnly="true" HeaderText="S.No" DataField="S.No." SortExpression="SNo">
<ItemStyle HorizontalAlign="Center" Width="2%" />
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Width="2%"/>
</asp:BoundField>
<asp:TemplateField ItemStyle-Width="3%" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="chkRow" runat="server" OnCheckedChanged="chkRow_CheckedChanged" AutoPostBack="true"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ReadOnly="true" HeaderText="Customer Id" ItemStyle-HorizontalAlign="Center" DataField="CustomerID" SortExpression="CustomerID">
<ItemStyle HorizontalAlign="Center" Width="10%" />
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Width="10%"/>
</asp:BoundField>
<asp:BoundField ReadOnly="true" HeaderText="Firm's Name" ItemStyle-HorizontalAlign="Center" DataField="Firm's Name" SortExpression="SNo">
<ItemStyle HorizontalAlign="Center" Width="20%" />
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Width="20%"/>
</asp:BoundField>
<asp:BoundField ReadOnly="true" HeaderText="Retailer Name" ItemStyle-HorizontalAlign="Center" DataField="Retialer Name" SortExpression="SNo">
<ItemStyle HorizontalAlign="Center" Width="20%" />
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Width="20%"/>
</asp:BoundField>
<asp:BoundField ReadOnly="true" HeaderText="Pesticide Licence No." ItemStyle-HorizontalAlign="Center" DataField="Pesticide Licence No" SortExpression="SNo">
<ItemStyle HorizontalAlign="Center" Width="10%" />
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Width="10%"/>
</asp:BoundField>
<asp:TemplateField HeaderText="Channel Partner-1" HeaderStyle-Font-Bold="true" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:DropDownList ID="ddlCP1" Enabled="false" ToolTip="Please Click on the checkbox to change the prefered CP" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCP1_SelectedIndexChanged"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Channel Partner-2" HeaderStyle-Font-Bold="true" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:DropDownList ID="ddlCP2" Enabled="false" ToolTip="Please Click on the checkbox to change the prefered CP" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCP2_SelectedIndexChanged"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Button runat="server" ID="BtnUpdateRow" Text="Update" ToolTip="Update This Record?" OnClick="BtnUpdateRow_Click" BackColor="#336699" ForeColor="#ffffff"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
This is the updatebutton event
protected void BtnUpdateRow_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
GridViewRow grow = (GridViewRow)btn.NamingContainer;
//btn.
CheckBox chkbx = (CheckBox)grow.FindControl("chkRow");
if (chkbx.Checked != true)
{
labelErrormessage.Visible = true;
labelErrormessage.ForeColor = System.Drawing.Color.Red;
labelErrormessage.Text = "Please Check/Uncheck the CheckBox and Click Update";
return;
}
else
{
/*--Code to update the row's record in the edatabase.--*/
??
labelErrormessage.Visible = true;
labelErrormessage.ForeColor = System.Drawing.Color.Green;
labelErrormessage.Text = "Updated Succesfully";
return;
}
}
How do I read the row in which the button has been clicked and update its dropdownlist selected values in the database?
If you have some sort of identification data displayed in the gridview you could get the value from the row's cell like this grow.Cells[0].Text then find the record in the database and update its values.
You could also hide the id for example if this is information you dont want to show to the user. The row will still be located at index 0 but hidden.
Sample:
else
{
var record = queryDatabase(grow.Cells[0].Text);
//update record values
updateRecord(record);
labelErrormessage.Visible = true;
labelErrormessage.ForeColor = System.Drawing.Color.Green;
labelErrormessage.Text = "Updated Succesfully";
return;
}
Related
I have a problem that is most likely easy but I'm missing something small.
I have looked at numerous entries on Stack Overflow with the same question but I must be doing something wrong.
I am trying to get the value of a textbox in a GridView after typing into it. I want to take that value and update a SQL field.
Here is my GridView:
<asp:GridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="SALESID" HeaderStyle-HorizontalAlign="CENTER" HorizontalAlign="CENTER" CellPadding="4" ForeColor="#333333" GridLines="Both" AllowSorting="true" AllowPaging="true" PageSize="100" OnRowDataBound="ASPxGridView1_RowDataBound" OnSorting="ASPxGridView1_Sorting" OnRowUpdating="ASPxGridView1_RowUpdating">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<%-- 0 This is e.Row.Cells[0].Text --%>
<asp:BoundField DataField="SALESID" HeaderText="SALES ORDER" SortExpression="SALESID" HeaderStyle-CssClass="padLeft" HeaderStyle-Width="125" ItemStyle-Width="115">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<%-- 1 --%>
<asp:BoundField DataField="ITEMID" HeaderText="PART NUMBER" HeaderStyle-HorizontalAlign="Center" SortExpression="ITEMID" ItemStyle-Width="150" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<%-- 2 --%>
<asp:BoundField DataField="NAME" HeaderText="PART NAME" HeaderStyle-HorizontalAlign="Center" SortExpression="NAME" ItemStyle-Width="320">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<%-- 3 --%>
<asp:BoundField DataField="CUSTACCOUNT" HeaderText="ACC" HeaderStyle-HorizontalAlign="Center" SortExpression="CUSTACCOUNT">
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<%-- 4 --%>
<asp:BoundField DataField="SALESNAME" HeaderText="CUSTOMER" HeaderStyle-HorizontalAlign="Center" SortExpression="SALESNAME" ItemStyle-Width="350" >
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<%-- 5 --%>
<asp:BoundField DataField="SHIPPINGDATECONFIRMED" HeaderText="SHIPPING DATE" HeaderStyle-HorizontalAlign="Center" SortExpression="SHIPPINGDATECONFIRMED" ItemStyle-Width="100" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<%-- 6 --%>
<asp:BoundField DataField="QTYORDERED" HeaderText="QTY ORDERED" HeaderStyle-HorizontalAlign="Center" SortExpression="QTYORDERED" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<%-- 7 --%>
<asp:BoundField DataField="REMAINSALESPHYSICAL" HeaderText="QUANTITY REMAINED" HeaderStyle-HorizontalAlign="Center" SortExpression="REMAINSALESPHYSICAL" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<%-- 8 --%>
<asp:BoundField DataField="AVAILPHYSICAL" HeaderText="AVAIL PHYSICAL" HeaderStyle-HorizontalAlign="Center" SortExpression="AVAILPHYSICAL" ItemStyle-Width="80" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<%-- 9 --%>
<asp:HyperLinkField DataTextField="WMSLOCATIONID" HeaderText="LOCATION" HeaderStyle-HorizontalAlign="Center" DataNavigateURLFields="ITEMID" DataNavigateURLFormatString="~\Location.aspx?ITEMID={0}" Target="_blank" SortExpression="WMSLOCATIONID">
<ItemStyle HorizontalAlign="Center" />
</asp:HyperLinkField>
<%-- 10 --%>
<asp:HyperLinkField DataTextField="INPROCESS" HeaderText="IN PROCESS" HeaderStyle-HorizontalAlign="Center" DataNavigateUrlFields="ITEMID" DataNavigateUrlFormatString="~\InProcess.aspx?ITEMID={0}" Target="_blank" SortExpression="INPROCESS">
<ItemStyle HorizontalAlign="Left" />
</asp:HyperLinkField>
<%-- 11 --%>
<asp:BoundField DataField="PRD" HeaderText="PRD" HeaderStyle-HorizontalAlign="Center" SortExpression="PRD" ItemStyle-Width="135">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<%-- 12 --%>
<asp:TemplateField HeaderText="SHIPTODAY" ItemStyle-Width="90">
<ItemTemplate>
<asp:TextBox ID="SHIPTODAY" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<%-- 13 --%>
<asp:TemplateField HeaderText="NOTES" >
<ItemTemplate>
<asp:TextBox ID="NOTES" runat="server" TextMode="MultiLine"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField ButtonType="Button" HeaderText="" CommandName="Update" Text="Update" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#284775" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="False" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
Here is my gridview _RowUpdating method:
protected void ASPxGridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Debug.WriteLine("row updating");
int index = Convert.ToInt32(e.RowIndex);
Debug.WriteLine("Index: " + index);
GridViewRow selectedRow = ASPxGridView1.Rows[index];
TableCell itemIDSelected = selectedRow.Cells[1];
TableCell rowPRD = selectedRow.Cells[11];
TableCell rowShipToday = selectedRow.Cells[12];
TableCell rowNotes = selectedRow.Cells[13];
string itemIdSelectedText = itemIDSelected.Text;
string rowPRDSelected = rowPRD.Text;
string rowShipTodaySelected = rowShipToday.Text;
string test = ((TextBox)selectedRow.FindControl("NOTES")).Text;
string test1 = ((TextBox)ASPxGridView1.Rows[index].FindControl("NOTES")).Text;
TextBox test2 = ASPxGridView1.Rows[index].FindControl("NOTES") as TextBox;
string test2text = test2.Text;
//string rowNotesSelected = rowNotes.Text;
//Debug.WriteLine(itemIdSelectedText);
//Debug.WriteLine(itemIdSelectedText);
//Debug.WriteLine(rowPRDSelected);
//Debug.WriteLine(rowShipTodaySelected);
Debug.WriteLine(test1);
Debug.WriteLine(test);
Debug.WriteLine(test2text);
//Debug.WriteLine(selectedRow.Cells[0].Text);
//Debug.WriteLine(selectedRow.Cells[1].Text);
//Debug.WriteLine(selectedRow.Cells[2].Text);
//Debug.WriteLine(selectedRow.Cells[3].Text);
//Debug.WriteLine(selectedRow.Cells[4].Text);
//Debug.WriteLine(selectedRow.Cells[5].Text);
//Debug.WriteLine(selectedRow.Cells[6].Text);
//Debug.WriteLine(selectedRow.Cells[7].Text);
//Debug.WriteLine(selectedRow.Cells[8].Text);
//Debug.WriteLine(selectedRow.Cells[9].Text);
//Debug.WriteLine(selectedRow.Cells[10].Text);
Debug.WriteLine(selectedRow.Cells[11].Text);
//Debug.WriteLine(selectedRow.Cells[12].Text);
//Debug.WriteLine(selectedRow.Cells[13].Text);
Debug.WriteLine("-----------");
//Debug.WriteLine(rowShipTodaySelected);
//Debug.WriteLine(rowNotesSelected);
}
When I run this and I type something in, the debug writeline's show.
I know i'm on the correct line because the index is correct per the line I am clicking the Update button on.
Sample debug output from above looks like this:
row updating
Index: 5
D31948P01
PRD-00030521
PRD-00030521
I cannot seem to get the value of the NOTES TextBox field.
As you can see with my testing I've tried using a FindControl and I've tried having it as a TextBox and also trying to get it directly to a string.
I've set breakpoints and each time I do it the test variable is null.
I'm missing something and I do not know what.
To summarize:
On non-text fields I can pull the data from the field.
On TextBox fields I cannot even using the FindControl method.
What I want to do:
Get the TextBox field value in the GridView so I can update the database.
StackOverflow links I've tried before asking this question:
c# Get value (text) of a bound textbox in a gridview
How to get value of TextBox of GridView in C#?
Manipulate textbox on gridview C#
Get TextBox value from GridView cell
ASP.net C# Gridview ButtonField onclick event
Thanks in advance. I've spent over 6 hours of attempting this without results. I don't understand it well enough yet. If there are any tutorials or videos explaining GridView's please let me know what they are so I can understand better.
The reason why RowUpdating method cannot find the text inside the textbox is most likely because of the postback. Clicking update button causes a post back to server. Before ASPxGridView1_RowUpdating is called, there is an a call to re-build the gridview ie.: ASPxGridView1.DataBind(). To avoid this, wrap the databind in an if (!Page.IsPostBack)
Below is a basic working example
<asp:GridView ID="ASPxGridView1"
runat="server"
AutoGenerateColumns="False"
OnRowUpdating="ASPxGridView1_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="NOTES" >
<ItemTemplate>
<asp:TextBox ID="NOTES" runat="server" TextMode="MultiLine"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField ButtonType="Button" HeaderText="" CommandName="Update" Text="Update" />
</Columns>
</asp:GridView>
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// This code will not be reached if you click update button.
var data = new string[] { "a", "b", "c" };
ASPxGridView1.DataSource = data;
ASPxGridView1.DataBind();
}
}
protected void ASPxGridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int index = Convert.ToInt32(e.RowIndex);
GridViewRow selectedRow = ASPxGridView1.Rows[index];
TextBox txtNotes = (TextBox)selectedRow.FindControl("NOTES");
Debug.WriteLine(txtNotes.Text);
}
I have a custom gridview and I use that grid in too many pages and I want to set the SortExpression text for all columns inside that grid in code behained ; as you see in the following code I have do that by overriding the OnRowDataBound eventhandler .
The problem is I can't access the evaluated datafield expression text ("START_WORK_DATE" in my case) for template Fields like what I did in BoundField
<eska:GridView ID="gvEmployees" runat="server" CssClass="new-grid" PageSize="20"
AllowSorting="true" AllowPaging="True"
AutoGenerateColumns="False" Width="100%"
GridContainer="tdEmployees" DataKeyNames="ID"
RowHoverCssClass="HoverRow" ShowCheckBox="True"
OnPageIndexChanging="gvEmployees_PageIndexChanging"
OnSelectedIndexChanged="gvEmployees_SelectedIndexChanged"
meta:resourcekey="gvEmployeesResource1">
<PagerSettings FirstPageImageUrl="~/App_Themes/Images/First.gif" LastPageImageUrl="~/App_Themes/Images/last.gif"
Mode="NextPreviousFirstLast" NextPageImageUrl="~/App_Themes/Images/Next.gif"
PreviousPageImageUrl="~/App_Themes/Images/prev.gif"></PagerSettings>
<SelectedRowStyle CssClass="SelectedRow"></SelectedRowStyle>
<Columns>
<asp:BoundField meta:resourcekey="BoundFieldResource1">
<ItemStyle Width="2%" />
<HeaderStyle Width="2%" />
<FooterStyle Width="2%" />
</asp:BoundField>
<asp:BoundField DataField="EMP_NO" HeaderText="Employee No"
meta:resourcekey="BoundFieldResource3">
<HeaderStyle Width="10%"></HeaderStyle>
<ItemStyle Width="10%"></ItemStyle>
<FooterStyle Width="10%" />
</asp:BoundField>
<asp:BoundField DataField="NAME" HeaderText="Name" meta:resourcekey="BoundFieldResource4">
<HeaderStyle Width="34%"></HeaderStyle>
<ItemStyle Width="34%"></ItemStyle>
<FooterStyle Width="34%" />
</asp:BoundField>
<asp:TemplateField HeaderText="Hiring Date" meta:resourcekey="TemplateFieldResource1">
<ItemTemplate>
<asp:Label runat="server"Text='<%# Convert.ToDateTime(DataBinder.Eval(Container,
"DataItem.START_WORK_DATE")).ToString("dd-MM-yyyy") %>'
ID="lblHiringDateGrid"></asp:Label>
</ItemTemplate>
<HeaderStyle Width="10%"></HeaderStyle>
<ItemStyle Width="10%"></ItemStyle>
<FooterStyle Width="10%" />
</asp:TemplateField>
<asp:BoundField DataField="PHONE1" HeaderText="Phone 1"
meta:resourcekey="BoundFieldResource5">
<HeaderStyle Width="10%"></HeaderStyle>
<ItemStyle Width="10%"></ItemStyle>
<FooterStyle Width="10%" />
</asp:BoundField>
<asp:BoundField DataField="Position_Name" HeaderText="Position"
meta:resourcekey="BoundFieldResource6">
<HeaderStyle Width="20%"></HeaderStyle>
<ItemStyle Width="20%"></ItemStyle>
<FooterStyle Width="20%" />
</asp:BoundField>
<asp:BoundField DataField="STATUS_DESC" HeaderText="Status"
meta:resourcekey="BoundFieldResource7">
<HeaderStyle Width="12%"></HeaderStyle>
<ItemStyle Width="12%"></ItemStyle>
<FooterStyle Width="12%" />
</asp:BoundField>
<asp:BoundField meta:resourcekey="BoundFieldResource8">
<ItemStyle Width="2%" />
<HeaderStyle Width="2%" />
<FooterStyle Width="2%" />
</asp:BoundField>
</Columns>
</eska:GridView>
Code behind:
protected override void OnRowDataBound(GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 0; i < this.Columns.Count; i++)
{
if (this.Columns[i] is BoundField)
{
if (((BoundField)(this.Columns[i])).DataField != string.Empty)
{
this.Columns[i].SortExpression = ((BoundField)(this.Columns[i])).DataField;
}
}
else if (this.Columns[i] is TemplateField)
{
// ???
}
}
}
}
You can do some thing like this. Set a header template with link button.
<asp:TemplateField>
<HeaderTemplate>
<asp:LinkButton ID="LinkButtonEmpName" runat="server" Text="Employee Name" CommandName="Sort" CommandArgument="Employees">
</asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="LabelEmployee" runat="server" Text='<%# Bind("Employees") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBoxEmployee" runat="server"/>
</FooterTemplate>
And in row command event find command like this.
protected void grd_RowCommand(object sender, GridViewCommandEventArgs e){
if (e.CommandName.Equals("Sort"))
{
FilterExpression = e.CommandArgument.ToString() + " LIKE '%" + txtCaseNumber.Text + "%'";
BindGridView();
}}
See the complete example here.
http://www.codeproject.com/Questions/528664/HowplustoplusapplyplussortingplusinplusTemplateplu
I have a gridView which works nicely until I open a new window by clicking a button. When the new window is opened the values in one of the columns 'lblTotalPrice' gets erased. 'lblTotalPrice' is a template field, which is calculated in MealList_RowDataBound (quantity * price). The footer for the column, however is not affected when the new window is opened (also gets its value in MealList_RowDataBound).
Also the font-size of the data-bound columns are changed when the new window is opened.
Any ideas of what might cause this? When stepping through the code there are no other lines executed after btnEnvComment_Click
When gridView is databound again, everything is back to normal.
The gridView:
<rwg:BulkEditGridView ID="MealList" runat="server" AutoGenerateColumns="False" ShowFooter="True" GridLines="Vertical" CellPadding="4"
BackColor="White" DataKeyNames="ItemId,ProductId" SaveButtonID="SaveButton"
DataSourceID="SqlMealItems" style="float:left"
OnRowDataBound="MealList_RowDataBound" OnRowCreated="MealList_RowCreated" CssClass="gridView">
<AlternatingRowStyle CssClass="MealListItemAlt" BackColor="#a6dbed" />
<Columns>
<asp:BoundField DataField="ProductId" HeaderText="Prod #" HeaderStyle-Wrap="false" ReadOnly="True" >
<HeaderStyle Wrap="False" Width="60px"></HeaderStyle>
<ItemStyle HorizontalAlign="Right" />
</asp:BoundField>
<asp:BoundField DataField="ProductName" HeaderText="Namn" ReadOnly="True" ItemStyle-Wrap="False" >
<ItemStyle Width="150px" />
</asp:BoundField>
<asp:BoundField DataField="Quantity" HeaderText="Antal" ControlStyle-CssClass="numeric" >
<ControlStyle CssClass="numeric" Width="30px"></ControlStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="Enhet">
<ItemTemplate>
<asp:DropDownList ID="DropDownUnit" OnSelectedIndexChanged="DropDownUnit_SelectedIndexChanged"
AutoPostBack="False" runat="server" DataTextField="Unit" DataValueField="Unit">
</asp:DropDownList>
</ItemTemplate>
<ControlStyle Width="70px" />
</asp:TemplateField>
<asp:BoundField HeaderText="á pris" ReadOnly="True" ControlStyle-CssClass="rightAlign" ItemStyle-Wrap="False">
<ControlStyle CssClass="numeric" ></ControlStyle>
<ItemStyle HorizontalAlign="Right" Width="80px" />
</asp:BoundField>
<asp:TemplateField HeaderText="Totalt" ItemStyle-Wrap="False">
<ItemTemplate>
<asp:label ID="lblTotalPrice" runat = server></asp:label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" Width="80px" />
<FooterTemplate>
<asp:Label ID="lblTotalPricePerMeal" runat="server"></asp:Label>
</FooterTemplate>
<FooterStyle HorizontalAlign="Right" Font-Bold="true" Font-Overline="True" Wrap="False" />
</asp:TemplateField>
<asp:BoundField DataField="Unit" HeaderText="h" ReadOnly="True" />
<asp:TemplateField>
<ItemTemplate>
<asp:image runat="server" ID="imgBlob" Width="24"></asp:image>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="lblError"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle CssClass="MealListFooter" />
<HeaderStyle CssClass="MealListHead" />
</rwg:BulkEditGridView>
The code for the button that open the new window:
protected void btnEnvComment_Click(object sender, EventArgs e)
{
int offsetPos = 125; // set placement of new window
String URL = "EnvComment.aspx?MealID=" + Session["MealId"];
String responsCommand = "window.open('" + URL + "','_blank','height=400,width=300,top=" + ",left=" + offsetPos.ToString() + offsetPos.ToString() + ",titlebar=1')";
Response.Write("<script>");
Response.Write(responsCommand);
Response.Write("</script>");
}
i'm using a GridView and trying to sort and page it.
I have more than 20 rows on my DataBase, but the GridView just shows 10.
I set AllowPaging=true but noting happened.
Also I'm using AllowSorting = true and OnSorting="GridView_Sorting". But when I click on the header to sort the the content of that column, it does not enters on my OnSorting="GridView_Sorting" it goes right to the GridView1_RowCommand why ?
Sometimes it just give me this error:
Object reference not set to an instance of an object error
Here's my Code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" style="font-family: Verdana, Arial, Sans-Serif;"
CssClass="gridview" OnSorting="GridView_Sorting"
AllowSorting ="True" AllowPaging="True" BackColor="#CCCCCC"
BorderStyle="Inset" BorderWidth="2px" BorderColor="GrayText"
CellPadding="1"
CellSpacing="5"
HeaderStyle-HorizontalAlign="Center"
OnRowDataBound="GridView1_RowDataBound"
ForeColor = "Black" RowStyle-CssClass="gridview"
OnRowCommand="GridView1_RowCommand">
<AlternatingRowStyle BackColor="#CCCCCC" />
<columns>
<asp:BoundField HeaderText="ID" DataField="id" SortExpression="id" />
<asp:BoundField HeaderText="PRIORIDADE" DataField="prioridade" ItemStyle-HorizontalAlign="Center" SortExpression="prioridade" SortExpression="prioridade" />
<asp:BoundField HeaderText="SITUAÇÃO" DataField="situacao" ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField HeaderText="RESPONSAVEL" DataField="responsavel" HeaderStyle-Width="65px" ItemStyle-HorizontalAlign="Center">
<HeaderStyle Width="65px" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField HeaderText="DATA DE CADASTRO" DataField="dt_cadastro" SortExpression="dt_cadastro" DataFormatString="{0:dd/MM/yyyy}" HeaderStyle-Width="60px"ItemStyleHorizontalAlign="Center" >
<HeaderStyle Width="60px" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField HeaderText="PREVISÃO DE TÉRMINO" DataField="previsao_termino" DataFormatString="{0:dd/MM/yyyy}" HeaderStyle-Width="60px"
ItemStyle-HorizontalAlign="Center">
<HeaderStyle Width="60px" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField HeaderText="PROJETO" DataField="projeto" ItemStyle-HorizontalAlign="Center"></asp:BoundField>
<asp:BoundField HeaderText="FUNCIONALIDADE" DataField="funcionalidade" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField HeaderText="CLUBE" DataField="clube" ItemStyle-HorizontalAlign="Center" />
<asp:TemplateField HeaderStyle-Width="70px" HeaderText="VISUALIZAR" >
<ItemTemplate>
<asp:Button ID="Btn_Visualizar" runat="server" Text="VISUALIZAR" CssClass="Btn_Grid" Font-Size="7pt" Font-Names="Verdana, Arial" OnClick="Btn_Visualizar_Click"CommandName="visualizar" CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="66px" HeaderText="ALTERAR">
<ItemTemplate>
<asp:Button ID="Btn_Alterar" runat="server" Text="ALTERAR" CssClass="Btn_Grid" Font-Size="7pt" Font-Names="Verdana, Arial"CommandName="editar" CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="66px" HeaderText="FEEDBACK">
<ItemTemplate>
<asp:Button ID="Btn_Feedback" runat="server" Text="ADICIONAR" CssClass="Btn_Grid" Font-Size="7pt" Font-Names="Verdana,Arial"CommandName="feedback" CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" />
</ItemTemplate>
</asp:TemplateField>
</columns>
My CodeBehind:
protected void GridView_Sorting(object sender, GridViewSortEventArgs e)
{
string[] strSortExpression = ViewState["SortExpression"].ToString().Split(' ');
// If the sorting column is the same as the previous one,
// then change the sort order.
if (strSortExpression[0] == e.SortExpression)
{
if (strSortExpression[1] == "ASC")
{
ViewState["SortExpression"] = e.SortExpression + " " + "DESC";
}
else
{
ViewState["SortExpression"] = e.SortExpression + " " + "ASC";
}
}
// If sorting column is another column,
// then specify the sort order to "Ascending".
else
{
ViewState["SortExpression"] = e.SortExpression + " " + "ASC";
}
// Rebind the GridView control to show sorted data.
GridView1.DataSource = ch.BuscaTodosChamados();
GridView1.DataBind();
}
Add SortExpression attribute for each sortable column, informing its DataField.
I have gridview which contains 60 rows and each row has 4 radiobutton option (select any one). To opearate that code , on submit button wrote code as follows . I didn't work.
My design view :
<asp:GridView ID="gvSurvey" runat="server" CellPadding = "4" OnRowDataBound ="gvSurvey_RowDataBound" BorderWidth ="2"
AutoGenerateColumns="False" EmptyDataText="No data Available" GridLines="None"
HorizontalAlign="Center" ForeColor="#333333" Font-Names="Verdana" ShowFooter="True">
<RowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="HEADER" HeaderText="Description" ItemStyle-HorizontalAlign="Left"
FooterStyle-HorizontalAlign="Left" >
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="QTN_NO" HeaderText="Description" ItemStyle-HorizontalAlign="Left"
FooterStyle-HorizontalAlign="Left" >
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="SQTN_NO" HeaderText="Description" ItemStyle-HorizontalAlign="Left"
FooterStyle-HorizontalAlign="Left" >
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:TemplateField Visible="true" HeaderText ="Strongly Disagree">
<ItemTemplate>
<asp:RadioButton ID="rdbtn1" runat ="server" GroupName ="MyRadioGroup"/>
<%-- <asp:CustomValidator ID="cvRadioButtonGroup" runat="server" ErrorMessage="* make a selection" onservervalidate="cvRadioButtonGroup_ServerValidate" /> --%>
<%--<input name="MyRadioButton1" type ="radio" value = "'<%# Eval("SQTN_NO") %>'" />--%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="true" HeaderText ="Agree">
<ItemTemplate>
<asp:RadioButton ID="rdbtn2" runat ="server" GroupName ="MyRadioGroup"/>
<%--<asp:CustomValidator ID="cvRadioButtonGroup" runat="server" ErrorMessage="* make a selection" onservervalidate="cvRadioButtonGroup_ServerValidate" /> --%>
<%--<input name="MyRadioButton2" type ="radio" value = "'<%# Eval("SQTN_NO") %>'" />--%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="true" HeaderText ="Disagree">
<ItemTemplate>
<asp:RadioButton ID="rdbtn3" runat ="server" GroupName ="MyRadioGroup"/>
<%-- <asp:CustomValidator ID="cvRadioButtonGroup" runat="server" ErrorMessage="* make a selection" onservervalidate="cvRadioButtonGroup_ServerValidate" /> --%>
<%--<input name="MyRadioButton3" type ="radio" value = "'<%# Eval("SQTN_NO") %>'" />--%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="true" HeaderText ="Strongly Agree">
<ItemTemplate>
<asp:RadioButton ID="rdbtn4" runat ="server" GroupName ="MyRadioGroup"/>
<%-- <asp:CustomValidator ID="cvRadioButtonGroup" runat="server" ErrorMessage="* make a selection" onservervalidate="cvRadioButtonGroup_ServerValidate" /> --%>
<%-- <input name="MyRadioButton4" type ="radio" value = "'<%# Eval("SQTN_NO") %>'" />--%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
C#
for (i = 0; i < gvSurvey.Rows.Count; i++)
{
RadioButton rdbtn1 = (RadioButton)gvSurvey.Rows[i].Cells[3].FindControl("rdbtn1");
RadioButton rdbtn2 = (RadioButton)gvSurvey.Rows[i].Cells[4].FindControl("rdbtn2");
RadioButton rdbtn3 = (RadioButton)gvSurvey.Rows[i].Cells[5].FindControl("rdbtn3");
RadioButton rdbtn4 = (RadioButton)gvSurvey.Rows[i].Cells[6].FindControl("rdbtn4");
// RadioButton rdbtn1=(RadioButton) gvSurvey.Rows[i].ClientID
qtn_no = dtSurvey.Rows[i]["QTN_NO"].ToString();
sqtn_no = dtSurvey.Rows[i]["SQTN_NO"].ToString();
if (rdbtn1.Checked)
{
rdbtn1.Text = "1";
lblMsg.Text = nominationsBiz.SaveSuggestion(ticketNo.ToString(), qtn_no, sqtn_no, rdbtn1.Text);
}
Please Go through the following code you will find the Solutions
// It will be on your submit button click
protected void getPinCode()
{
foreach (GridViewRow grdRows in gvSurvey.Rows)
{
RadioButton rbt1 = (RadioButton)grdRows.FindControl("rdbtn1");
RadioButton rbt2 = (RadioButton)grdRows.FindControl("rdbtn2");
RadioButton rbt3 = (RadioButton)grdRows.FindControl("rdbtn3");
RadioButton rbt4 = (RadioButton)grdRows.FindControl("rdbtn4");
string Value = RadioValue(rbt1);
//Similarly you can do for all radio button
if(!string.IsNullOrEmpty(Value))
{
lblMsg.Text = nominationsBiz.SaveSuggestion(ticketNo.ToString(), qtn_no, sqtn_no, Value);
}
}
}
protected string RadioValue(RadioButton Rbtlst)
{
string Value = "";
if (Rbtlst.Checked == true)
{
Value = Rbtlst.Text;
}
return Value;
}