Hello i have a Gridview with 4 radio buttons and i want to get the value from them, and no matter what i do the value is always false, could someone tellme where is my mistake?
This is the code of the gridview:
<asp:GridView ID="GridView8" runat="server" Width="903px"
Height="516px" CellPadding="4" ForeColor="#333333" GridLines="None"
Visible="False"
>
<AlternatingRowStyle BorderColor="Black" BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Solicitante/">
<ItemTemplate>
<asp:RadioButton ID="optCl1" runat="server" Text="SI" GroupName="optCl" />
<asp:RadioButton ID="optCl2" runat="server" Text="NO" GroupName="optCl" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CoGarante">
<ItemTemplate >
<asp:RadioButton ID="optGar1" runat="server" Text="SI" GroupName="optGar" />
<asp:RadioButton ID="optGar2" runat="server" Text="NO" GroupName="optGar" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BorderColor="Black" />
<FooterStyle BackColor="#990000" BorderColor="Black" ForeColor="White"
Font-Bold="True" />
<HeaderStyle BackColor="#990000" BorderColor="Black" Font-Bold="True"
ForeColor="White" />
<PagerStyle ForeColor="#333333" HorizontalAlign="Center" BackColor="#FFCC66" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
The code of the function that read the radiobutton
protected void saveQuestions()
{
foreach (GridViewRow row in GridView8.Rows)
{
RadioButton rb = row.Cells[2].FindControl("optGar2") as RadioButton;
Response.Write(rb.Checked);
}
conn.Close();
}
The code of the function that set the data on the gridview:
protected void loadQuestions()
{
OdbcConnection conn = connection();
conn.Open();
OdbcCommand findSql = new OdbcCommand("SELECT question AS PREGUNTAS,id FROM questionary_reg WHERE(status='1')", conn);
GridView8.DataSource = null;
DataTable dt = new DataTable();
dt.Load(findSql.ExecuteReader());
GridView8.DataSource = dt;
GridView8.DataBind();
conn.Close();
}
The problem because happen postback and reset the values inside the gridview, make sure you
call this function loadQuestions() on if !Postback ONLY
if(!IsPostBack){
loadQuestions();
}
#UPDATE 1 WORKING CODE :
//Design
<asp:GridView runat="server" ID="gv">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButton runat="server" ID="rd" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button runat="server" ID="btn" onclick="btn_Click" />
//Code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStringDb1"].ToString()))
{
try
{
String cmdText = "SELECT * FROM Image WHERE IsDeleted=#isDeleted";
SqlCommand cmd = new SqlCommand(cmdText, cn);
cmd.Parameters.AddWithValue("#IsDeleted", "false");
cn.Open();
SqlDataAdapter myAdapter = new SqlDataAdapter(cmd);
DataTable dt_Category = new DataTable();
myAdapter.Fill(dt_Category);
cn.Close();
gv.DataSource = dt_Category;
gv.DataBind();
}
catch (Exception ex)
{
}
}
}
}
protected void btn_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvr in gv.Rows)
{
RadioButton rd = (RadioButton)gvr.FindControl("rd");
if (rd.Checked)
{
}
else
{
}
}
}
Maybe you need a 'CheckedChanged' event: (Tested and working)
In ASPX set (in this example, you can to see label display the number of row selected)
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButton ID="rbtnSelect" AutoPostBack="true" runat="server" OnCheckedChanged="rbtnSelect_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
and code-behing set
protected void rbtnSelect_CheckedChanged(object sender, EventArgs e)
{
RadioButton selectButton = (RadioButton)sender;
GridViewRow row = (GridViewRow)selectButton.Parent.Parent;
int a = row.RowIndex;
foreach (GridViewRow rw in gvCursos.Rows)
{
if (selectButton.Checked)
{
if (rw.RowIndex != a)
{
lbResultado.Text = rw.RowIndex.ToString();
RadioButton rd = rw.FindControl("rbtnSelect") as RadioButton;
rd.Checked = false;
}
}
}
}
Change this:
RadioButton rb = row.Cells[2].FindControl("optGar2") as RadioButton;
To this:
RadioButton rb = row.FindControl("optGar2") as RadioButton;
for (int i = 0; i < GridView8.Rows.Count; i++)
{
if (GridView8.Rows[i].RowType == DataControlRowType.DataRow)
{
RadioButton rb= (RadioButton)grdView.Rows[i].FindControl("optGar2");
Response.Write(rb.Checked);
}
}
foreach (GridViewRow gvp in gridView1.Rows)
{
System.Web.UI.HtmlControls.HtmlInputRadioButton rd = (System.Web.UI.HtmlControls.HtmlInputRadioButton)gvp.FindControl("rd");
if (rd.Checked)
{
string s = rd.Value;
}
else
{
}
}
design view
<ItemTemplate>
<input runat="server" id='rd' type="radio" value='<%# Eval("id") %>' onclick="javascript:SelectSingleRadiobutton(this.id)" />
</ItemTemplate>
Related
This question already has answers here:
How to delete row in gridview using rowdeleting event?
(16 answers)
Closed 8 years ago.
I want to delete a row using OnRowDeleting on GridView..
I have tried Different codes from internet but nothing works for me..
Help me Please!!
This is my code:
ASPX file::
<script type="text/c#" runat="server">
protected void BtnUpload_Click(object sender, EventArgs e)
{
if (Request.Files != null)
{
foreach (string file in Request.Files)
{
var uploadedFile = Request.Files[file];
if (uploadedFile.ContentLength > 0)
{
var appData = Server.MapPath("~/");
var fileName = Path.GetFileName(uploadedFile.FileName);
uploadedFile.SaveAs(Path.Combine(appData, fileName));
}
}
}
} //Some code here
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" OnRowDeleting="GridView1_RowDeleting" CellPadding="4" ForeColor="#333333" GridLines="None" Width="155px">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField >
<ItemTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField CommandName="Uploader" ButtonType="Button" Text="Upload" HeaderText="Upload" />
<asp:CommandField ShowDeleteButton="True" ButtonType="Button" HeaderText="Delete" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="Add New Row" OnClick="Button1_Click" />
<asp:Panel ID="pnlInfo" runat="server">
</asp:Panel>
<asp:LinkButton ID="BtnUpload" runat="server" Text="Upload" OnClick="BtnUpload_Click" />
//some code here
and this is ASPX.CS
public partial class WebForm4 : System.Web.UI.Page
{
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs ea)
{
if(ea.CommandName=="Uploader")
{
//GridViewRow grv = (GridViewRow);
int i = Convert.ToInt32(ea.CommandArgument);
FileUpload fileUpload = GridView1.Rows[i].FindControl("FileUpload1") as FileUpload;
if (fileUpload.HasFile)
{
try
{
string filename = Path.GetFileName(fileUpload.FileName);
fileUpload.SaveAs(Server.MapPath("~/") + filename);
}
catch (Exception ex)
{
String se = ex.Message;
}
}
}
}
protected virtual void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs ea)
{
//Suggest Code Here..
}
private void AddNewRow()
{
dt = new DataTable();
dt.Columns.Add("sno");
dt.Columns.Add("name");
foreach (GridViewRow gvRow in GridView1.Rows)
{
DataRow dr = dt.NewRow();
//dr["sno"] = ((Label)gvRow.FindControl("lblSno")).Text;
//dr["name"] = ((Label)gvRow.FindControl("txtName")).Text;
dt.Rows.Add(dr);
}
DataRow dr1 = dt.NewRow();
dr1["sno"] = "";
dr1["name"] = "";
dt.Rows.Add(dr1);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
AddNewRow();
}
}
}
Please Help...
I'd recommend you to work with the bound data while adding/deleting rows, not with the rows directly since you've chosen grid view. Code is much simpler then.
Below is an example - note that I initialize there DataTable in a static scope (which is not a perfect place) - store it where you need - in memory/session, or in view state - depends on your task there.
public partial class WebForm4 : System.Web.UI.Page
{
private static DataTable MyDataTable = new DataTable();
static WebForm4()
{
MyDataTable.Columns.Add("sno");
MyDataTable.Columns.Add("name");
}
private void AddNewRow()
{
MyDataTable.Rows.Add(MyDataTable.NewRow());
GridView1.DataSource = MyDataTable;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
AddNewRow();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
MyDataTable.Rows.RemoveAt(e.RowIndex);
GridView1.DataSource = MyDataTable;
GridView1.DataBind();
}
}
Hope that helped.
I have a gridview with textboxes for item no, desc qty, cost, extncost. When itemno entered in the textbox desc and cost of the item will comes automatically by on textbox event change.
Here when i add a new row in the grid view, values of last entered value got disappears.
when i am checking using break point i can able to see the last entered values in data table.
Since, while adding new row has blank text boxes, now system considers blank text box for on text change event. So, last entered values also not displaying. Text box changes in one row also affects in other rows.
Here is the ASPX.page code:
<asp:UpdatePanel ID="gin_pnlupdt" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:GridView ID="grv_gindet" runat="server" ShowFooter="True" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDeleting="grvStudentDetails_RowDeleting"
OnRowDataBound="grv_gindtrowcmd" OnRowCommand="grv_gindetrowcmd">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="SNo" />
<asp:TemplateField HeaderText="Item Number">
<ItemTemplate>
<asp:TextBox ID="txt_itemno" runat="server" OnTextChanged="txt_itemno_changed" AutoPostBack="True"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Item Description">
<ItemTemplate>
<asp:Label ID="txt_itemdesc" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="txt_qty" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Unit Cost">
<ItemTemplate>
<asp:Label ID="txt_ucost" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Extended Cost">
<ItemTemplate>
<asp:Label ID="txt_extncost" runat="server"></asp:Label>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="BtnAddRow" />
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" />
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="grv_gindet" />
</Triggers>
</asp:UpdatePanel>
Here is the CS code for add new row:
protected void AddNewRow()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
TextBox TextBoxItem =
(TextBox)grv_gindet.Rows[rowIndex].Cells[1].FindControl("txt_itemno");
Label TextBoxDesc =
(Label)grv_gindet.Rows[rowIndex].Cells[2].FindControl("txt_itemdesc");
TextBox TextBoxQty =
(TextBox)grv_gindet.Rows[rowIndex].Cells[3].FindControl("txt_qty");
Label TextBoxucost =
(Label)grv_gindet.Rows[rowIndex].Cells[4].FindControl("txt_ucost");
Label TextBoxextncost =
(Label)grv_gindet.Rows[rowIndex].Cells[5].FindControl("txt_extncost");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;
dtCurrentTable.Rows[i - 1]["Col1"] = TextBoxItem.Text;
dtCurrentTable.Rows[i - 1]["Col2"] = TextBoxDesc.Text;
dtCurrentTable.Rows[i - 1]["Col3"] = TextBoxQty.Text;
dtCurrentTable.Rows[i - 1]["Col4"] = TextBoxucost.Text;
dtCurrentTable.Rows[i - 1]["Col5"] = TextBoxextncost.Text;
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
grv_gindet.DataSource = dtCurrentTable;
grv_gindet.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
SetPreviousData();
}
Code for retrive Previous data:
private void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
TextBox TextBoxItem = (TextBox)grv_gindet.Rows[rowIndex].Cells[1].FindControl("txt_itemno");
Label TextBoxDesc = (Label)grv_gindet.Rows[rowIndex].Cells[2].FindControl("txt_itemdesc");
TextBox TextBoxQty =
(TextBox)grv_gindet.Rows[rowIndex].Cells[3].FindControl("txt_qty");
Label TextBoxucost =
(Label)grv_gindet.Rows[rowIndex].Cells[4].FindControl("txt_ucost");
Label TextBoxextncost =
(Label)grv_gindet.Rows[rowIndex].Cells[5].FindControl("txt_extncost");
TextBoxItem.Text = dt.Rows[i]["Col1"].ToString();
TextBoxDesc.Text = dt.Rows[i]["Col2"].ToString();
TextBoxQty.Text = dt.Rows[i]["Col3"].ToString();
TextBoxucost.Text = dt.Rows[i]["Col4"].ToString();
TextBoxextncost.Text = dt.Rows[i]["Col5"].ToString();
rowIndex++;
}
}
}
}
Textbox change event:
protected void txt_itemno_changed(object sender, EventArgs e)
{
//TextBox thisTextBox = (TextBox)sender;
//GridViewRow thisGridViewRow = (GridViewRow)thisTextBox.Parent.Parent;
//int row = thisGridViewRow.RowIndex;
GridViewRow currentrow = (GridViewRow)((TextBox)sender).Parent.Parent;
TextBox thisTextBox = (TextBox)currentrow.FindControl("txt_itemno");
int row = currentrow.RowIndex;
//rowChanged[row] = true;
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AWCC"].ConnectionString))
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT ITEMDET.ITEMDESC,RGITEMDET.UNITCOST FROM ITEMDET JOIN RGITEMDET ON RGITEMDET.ITEMNO=ITEMDET.ITEMNO WHERE ITEMDET.ITEMNO ='" + thisTextBox.Text + "' ", con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
grv_gindet.Rows[row].Cells[2].Text = dr["ITEMDESC"].ToString();
grv_gindet.Rows[row].Cells[4].Text = dr["UNITCOST"].ToString();
}
}
thisTextBox.Enabled = false;
}
Postback control code for gridview child element:
protected void grv_gindtrowcmd(object sender, GridViewRowEventArgs e)
{
try
{
TextBox txtitm = e.Row.FindControl("txt_itemno") as TextBox;
LinkButton lnkbtn = e.Row.FindControl("ShowDeleteButton") as LinkButton;
ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(txtitm);
ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(lnkbtn);
}
catch
{
}
}
protected void grv_gindetrowcmd(object sender, GridViewCommandEventArgs e)
{
try
{
Button btnad = grv_gindet.FooterRow.FindControl("ButtonAdd") as Button;
ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(btnad);
}
catch
{
}
}
Kinldy provide a solution ASAP, do the needful.
I hope this will help you, change this in Textchange event
GridViewRow currentrow = (GridViewRow)((TextBox)sender).Parent.Parent.Parent.Parent;
TextBox thisTextBox = (TextBox)currentrow.FindControl("txt_itemno");
if (!string.IsNullOrWhiteSpace(thisTextBox.Text))
{
int row = currentrow.RowIndex;
//rowChanged[row] = true;
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AWCC"].ConnectionString))
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT ITEMDET.ITEMDESC,RGITEMDET.UNITCOST FROM ITEMDET JOIN RGITEMDET ON RGITEMDET.ITEMNO=ITEMDET.ITEMNO WHERE ITEMDET.ITEMNO ='" + thisTextBox.Text + "' ", con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
((Label)(grv_gindet.Rows[row].FindControl("txt_itemdesc"))).Text = dr["ITEMDESC"].ToString();
((TextBox)(grv_gindet.Rows[row].FindControl("txt_ucost"))).Text = dr["UNITCOST"].ToString();
}
con.Close();
}
thisTextBox.Enabled = false;
}
i have a checkbox in a gridview , i want to display row's data in textbox when i check o checkbox .this is my buttun's code :
protected void ButtonModifEnfant_Click(object sender, EventArgs e)
{
#region
try
{
int nbr_check = 0;
string myid = "";
for (int i = 0; i < gv_enfant.Rows.Count; i++)
{
CheckBox check = (CheckBox)gv_enfant.Rows[i].Cells[0].FindControl("CheckBoxenfant");
if (check.Checked)
{
myid = ((HiddenField)gv_enfant.Rows[i].Cells[0].FindControl("codeenfant")).Value;
//ButtonModifEnfant.Enabled = true;
//ButtonSuppEnfant.Enabled = true;
//myid = gv_enfant.Rows[i].Cells[0].Text;
//nbr_check++;
}
else
{
//ButtonModifEnfant.Enabled = false;
//ButtonSuppEnfant.Enabled = true;
}
}
if (nbr_check >= 1)
{
//ButtonModifEnfant.Enabled = true;
Response.Write("<script>alert ('Error!')</script>");
return;
}
else if (nbr_check == 1)
{
//ButtonModifEnfant.Enabled = true;
}
ModalPopupExtenderModifierEnfant.Show();
c.cmd = c.cn.CreateCommand();
c.cmd.CommandText = "select prenom , DateNaissance , Scolarise , Activite from Enfants where codeEnfants = " + myid;
if (c.cn.State == ConnectionState.Closed)
{
c.cn.Open();
}
SqlDataReader read = c.cmd.ExecuteReader();
if (read.HasRows)
{
read.Read();
TextBox_NPmodif.Text = read[0].ToString();
TextBox_DNmodif.Text = read[1].ToString();
TextBox_Scolarisemodif.Text = read[2].ToString();
TextBox_Activitemodif.Text = read[3].ToString();
}
read.Close();
//gv_enfant.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
if (c.cn.State == ConnectionState.Open)
{
c.cn.Close();
}
}
#endregion
}
and this is my checkbox code :
protected void CheckBoxEnfants_CheckedChanged(object sender, EventArgs e)
{
int chek = 0;
for (int i = 0; i < gv_enfant.Rows.Count; i++)
{
CheckBox chbox = (CheckBox)gv_enfant.Rows[i].Cells[0].FindControl("CheckBoxenfant");
if (chbox.Checked)
{
chek++;
}
}
if (chek != 1)
{
ButtonModifEnfant.Enabled = false;
ButtonSuppEnfant.Enabled = false;
}
else
{
ButtonModifEnfant.Enabled = true;
ButtonSuppEnfant.Enabled = true;
}
}
and for the html code of gridview :
<asp:GridView ID="gv_enfant" runat="server" AutoGenerateColumns="False" BackColor="White"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataSourceID="SqlDataSource1"
Width="533px">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="CheckBoxenfant" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBoxEnfants_CheckedChanged" />
<asp:HiddenField ID="codeenfant" runat="server" Value='<%# Eval("codeEnfants") %>' />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:BoundField DataField="prenom" HeaderText="prenom" SortExpression="prenom" />
<asp:BoundField DataField="DateNaissance" HeaderText="Date Naissance" DataFormatString="{0:d}"
SortExpression="DateNaissance" />
<asp:BoundField DataField="Scolarise" HeaderText="Scolarise" SortExpression="Scolarise" />
<asp:BoundField DataField="Activite" HeaderText="Activite" SortExpression="Activite" />
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CVtechConnectionString10 %>"
SelectCommand="SELECT * FROM [Enfants] WHERE ([ppr] = #ppr)">
<SelectParameters>
<asp:SessionParameter Name="ppr" SessionField="code" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
Thank you
This may help you.
if (check.Checked)
{
txtBox.Text=gv_enfant["yourColumnName",i].Value.ToString();
}
try it,
Front-End
<asp:CheckBox ID="CheckBoxenfant" runat="server" index="<%# Container.DataItemIndex %>" AutoPostBack="true" OnCheckedChanged="CheckBoxEnfants_CheckedChanged" />
<asp:TextBox ID="textbx" runat="server" Enabled="false" Text='<%# Eval("codeEnfants") %>' />
Code-behind
protected void CheckBoxEnfants_CheckedChanged(object s, EventArgs e)
{
byte index = byte.Parse((s as CheckBox).Attributes["index"]);
(gv_enfant.Rows[index].FindControl("textbx") as TextBox).Enabled = (gv_enfant.Rows[index].FindControl("CheckBoxenfant") as CheckBox).Checked;
}
I have a C# .Net application with a gridview within an Ajax Modal Popup (VS2008). I have the grid view set to return 10 records per page with paging enabled.
When the user clicks to change page within the gridview there is a postback which closes the modal window and then opens it again using ModalPopup.show();
Is there any way to avoid the postback of the whole page and just postback the gridview whilst keeping the modal window active? At the moment the postback of the whole page gives the impression of flicker...
<asp:Panel ID="Panel1" runat="server" Font-Italic="True"
Font-Names="Times New Roman" Font-Size="Small" ForeColor="#82B8DE">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None"
onpageindexchanging="GridView1_PageIndexChanging"
onrowdatabound="GridView1_RowDataBound"
onselectedindexchanged="GridView1_SelectedIndexChanged"
SelectedIndex="0" ShowHeader="False" Width="700px" ControlID="GridView1"
EventName="PageIndexChanging" Font-Italic="True" Font-Names="Times New Roman"
Font-Size="Medium">
<PagerSettings PageButtonCount="12" />
<RowStyle CssClass="RowStyle" BackColor="#EFF3FB" Font-Italic="True"
Font-Names="Times New Roman" Font-Size="Small" ForeColor="#82B8DE" />
<Columns>
<asp:BoundField DataField="Address" ReadOnly="True">
<ItemStyle Width="385px" />
</asp:BoundField>
<asp:BoundField DataField="XCoord" ReadOnly="True" ShowHeader="False" >
<ItemStyle CssClass="Hidden" />
</asp:BoundField>
<asp:BoundField DataField="YCoord" ReadOnly="True" ShowHeader="False" >
<ItemStyle CssClass="Hidden" />
</asp:BoundField>
</Columns>
<FooterStyle CssClass="FooterStyle" BackColor="#507CD1" Font-Bold="True"
ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle CssClass="SelectedRowStyle" BackColor="#D1DDF1"
Font-Bold="True" ForeColor="#333333" />
<HeaderStyle CssClass="HeaderStyle" BackColor="#507CD1" Font-Bold="True"
ForeColor="White" />
<EditRowStyle BackColor="#2461BF" Font-Italic="True"
Font-Names="Times New Roman" Font-Size="Medium" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</asp:Panel>
<ajax:ModalPopupExtender ID="ModalPopupExtender1" runat="server" PopupControlID="Panel1" TargetControlID="dummy"
BackgroundCssClass="ModalBackgroundGrid" BehaviorID="ModalGrid">
</ajax:ModalPopupExtender>
And the code behind...
public void Page_Load(object sender, EventArgs e)
{
try
{
if (!(Page.IsPostBack))
{
GridView1.EnableViewState = true;
GridView1.AllowPaging = true;
GridView1.PageSize = 10;
GridView1.PagerSettings.Mode = PagerButtons.Numeric;
GridView1.Visible = true;
}
if (!m_bDisclaimerShown)
{
m_bDisclaimerShown = true;
mpe1.Show();
TabContainer.Visible = true;
ScaleBar1.Visible = true;
}
}
catch (Exception ex)
{
ShowMsg("Error - " + ex.Message);
}
}
protected void btnHide_Click(object sender, EventArgs e)
{
mpe1.Hide();
TabContainer.Visible = true;
ScaleBar1.Visible = true;
}
protected void cmdZoomAddress_Click(object sender, EventArgs e)
{
try
{
if (txtPostCode.Text.Length >= 7 && OpenDB())
{
string strPostcode = txtPostCode.Text;
strPostcode = strPostcode.Substring(0, 4) + strPostcode.Substring(strPostcode.Length - 3, 3);
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = m_sqlConn;
sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
sqlCmd.CommandText = "sde.dbo.sp_selAddressByPostcode";
sqlCmd.Parameters.Add("#Postcode", SqlDbType.VarChar);
sqlCmd.Parameters["#Postcode"].Value = strPostcode;
SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlCmd);
m_sqlDataTable = new DataTable();
sqlAdapter.Fill(m_sqlDataTable);
GridView1.DataSource = m_sqlDataTable;
GridView1.DataBind();
GridView1.Visible = true;
ModalPopupExtender1.Show();
}
else
{
ShowMsg("Error - No Postal Addresses Returned");
}
}
catch (Exception ex)
{
ShowMsg("Error - " + ex.Message);
}
finally
{
CloseDB();
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
if (sender != null)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = m_sqlDataTable;
GridView1.DataBind();
ModalPopupExtender1.Show();
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow GVRow = GridView1.SelectedRow;
int iX = (int)Convert.ToSingle(GVRow.Cells[1].Text);
int iY = (int)Convert.ToSingle(GVRow.Cells[2].Text);
GridView1.Visible = false;
MoveMap(iX, iY);
}
public void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.DataItemIndex >= 0)
{
e.Row.Attributes["style"] = "cursor:pointer";
e.Row.Attributes.Add("onMouseOver", "this.style.cursor='hand';");
e.Row.Attributes.Add("onclick", ClientScript.GetPostBackEventReference(GridView1, "Select$" + e.Row.RowIndex.ToString()));
}
}
protected override void Render(HtmlTextWriter writer)
{
foreach (GridViewRow r in GridView1.Rows)
{
if (r.RowType == DataControlRowType.DataRow)
{
Page.ClientScript.RegisterForEventValidation(GridView1.UniqueID, "Select$" + r.RowIndex);
}
}
base.Render(writer);
}
thanks for your suggestion. I've put the gridview into a...
<asp:UpdatePanel>
<ContentTemplate>
The modal now stays however when I click a record in the grid view it doesn't close!
The sorting works fine when u fill the gridview using SQL datasource in the aspx page...
but now i am using template field and the columns are filled separately in the codebehind and the sorting is not working...
my code is
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False"
ondatabound="GridView1_DataBound"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="File Name" ItemStyle-Width="40%" >
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server"></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Failure Count" ItemStyle-Width="10%" >
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server"></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
</Columns></GridView>
and my codebehind is:
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection();
connection.ConnectionString = ConfigurationManager.ConnectionStrings["SumooHAgentDBConnectionString"].ConnectionString;
connection.Open();
SqlCommand sqlCmd = new SqlCommand("SELECT FileName,FailureCount from Files where MachineID=#strID , connection);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
sqlCmd.Parameters.AddWithValue("strID", strID);
sqlDa.Fill(dt);
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
string nameoffiles = dt.Rows[i]["FileName"].ToString();
buFailureCode.Add(code);
string count = dt.Rows[i]["BuFailureCount"].ToString();
buFailureCount.Add(count);
}
}
connection.Close();
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
if (namesOfFiles.Count != 0)
{
for (int i = 0; i < namesOfFiles.Count; i++)
{
GridViewRow myRow = GridView1.Rows[i];
Label Label1 = (Label)myRow.FindControl("Label1");
Label Label3 = (Label)myRow.FindControl("Label3");
Label1.Text = namesOfFiles[i].ToString();
Label3.Text = buFailureCount[i].ToString();
}}}
set SortExpression
I think you'll have to handle the OnSorting event and do the actual sorting yourself in code-behind. The API documentation has an example.