ayha1999
asked on
GridView select row
Hi,
Im trying to change the value of lblXVal label control by changing the value in the textbox1 control (by firing textbox_changed event). When I change the value in the textbox its changes values in all lblXval controls in the grid instead of changing the lblXval for the current row where I changed the TextBox1 value.
How can I fix the the problem?
Ayha
Im trying to change the value of lblXVal label control by changing the value in the textbox1 control (by firing textbox_changed event). When I change the value in the textbox its changes values in all lblXval controls in the grid instead of changing the lblXval for the current row where I changed the TextBox1 value.
<asp:GridView ID="gvFiles" runat="server" AutoGenerateColumns="False" BackColor="White"
BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black"
GridLines="Vertical" OnRowDeleting="Delete" DataKeyNames="id" Width="682px">
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:TemplateField HeaderText="X" SortExpression="xval">
<ItemTemplate>
<asp:Label ID="lblXVal" runat="server" Text='<%# Bind("xval") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Scale">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text="1" AutoPostBack="true" ontextchanged="TextBox2_TextChanged"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
...
code
protected void TextBox2_TextChanged (object sender, EventArgs e)
{
for (int count = 0 ; count < gvFiles.Rows.Count ; count++)
{
string x = ((Label)gvFiles.Rows[count].FindControl ("lblXVal")).Text;
string s = ((TextBox)gvFiles.Rows[count].FindControl ("TextBox1")).Text;
Label t = ((Label)gvFiles.Rows[count].FindControl ("lblXVal"));
t.Text = (Convert.ToDouble (x) * Convert.ToDouble (s)).ToString ();
}
}
How can I fix the the problem?
Ayha
You're looping all rows. Instead of using for, just run it once and use e.RowIndex as the row number.
ASKER
how can I RowIndex? please give me the code.
Thanks
Thanks
Hi ayha1999
you need to make some changes in aspx code as below:
1. add a javascript in the head section of your page
<script type="text/javascript" language="javascript">
function DoPostBackWithRowIndex(row Index){
if (document.getElementById(' <%=HDVal.C lientID%>' ) != null) {
document.getElementById('< %=HDVal.Cl ientID%>') .value = rowIndex;
}
return true;
}
</script>
2.add a hidden field above your grid view as
<asp:HiddenField ID="HDVal" runat="server" />
3. Now add a row databound event to your gridview and do the code as follows:
protected void gvFiles_RowDataBound(objec t sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow )
{
TextBox TxtMarks = (TextBox)e.Row.FindControl ("TextBox1 ");
TxtMarks.Attributes.Add("o nkeydown", "javascript:return DoPostBackWithRowIndex('" + e.Row.RowIndex + "');");
}
}
4 .Now modify your Textchanged event as below
protected void TextBox2_TextChanged(objec t sender, EventArgs e)
{
if (HDVal.Value.Trim().Length == 0)
{
return;
}
else
{
int Rowindex= Convert.ToInt32(HDVal.Valu e);
string x = ((Label)gvFiles.Rows[Conve rt.ToInt16 (Rowindex) ].FindCont rol("lblXV al")).Text ;
string s = ((TextBox)gvFiles.Rows[Row index].Fin dControl(" TextBox1") ).Text;
Label t = ((Label)gvFiles.Rows[Rowin dex].FindC ontrol("lb lXVal"));
t.Text = (Convert.ToDouble(x) * Convert.ToDouble(s)).ToStr ing();
}
}
and it will work as expected.
hope this helps.
you need to make some changes in aspx code as below:
1. add a javascript in the head section of your page
<script type="text/javascript" language="javascript">
function DoPostBackWithRowIndex(row
if (document.getElementById('
document.getElementById('<
}
return true;
}
</script>
2.add a hidden field above your grid view as
<asp:HiddenField ID="HDVal" runat="server" />
3. Now add a row databound event to your gridview and do the code as follows:
protected void gvFiles_RowDataBound(objec
{
if (e.Row.RowType == DataControlRowType.DataRow
{
TextBox TxtMarks = (TextBox)e.Row.FindControl
TxtMarks.Attributes.Add("o
}
}
4 .Now modify your Textchanged event as below
protected void TextBox2_TextChanged(objec
{
if (HDVal.Value.Trim().Length
{
return;
}
else
{
int Rowindex= Convert.ToInt32(HDVal.Valu
string x = ((Label)gvFiles.Rows[Conve
string s = ((TextBox)gvFiles.Rows[Row
Label t = ((Label)gvFiles.Rows[Rowin
t.Text = (Convert.ToDouble(x) * Convert.ToDouble(s)).ToStr
}
}
and it will work as expected.
hope this helps.
ASKER
Hi patil786,
I tried followed you code exacty, it is firing but not changing the value of lblXVal.
Please check.
Thanks
I tried followed you code exacty, it is firing but not changing the value of lblXVal.
Please check.
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you so much.
ayha
ayha
ASKER
Hi patil,
Could you please have look the following question?
https://www.experts-exchange.com/questions/28161806/Datalist-selected-row.html
Thanks
ayha
Could you please have look the following question?
https://www.experts-exchange.com/questions/28161806/Datalist-selected-row.html
Thanks
ayha