I have a requirement where i have to update a textbox if any of the value in my grid view changes.. I have a gridview with 2 rows ..
one a template field with label and another template field with a textbox...
my Grid view looks like
name value
a (empty textbox)
b (empty textbox)
c (empty textbox)
now when the user enters a value in teh text box it should automatically update another textbox which is linked to this.
Here my questions is when someone enters a value in the textbox an event should be fired!
(I am getting the names a,b,c, from database). i dont want to have an edit link or update link because all the values to be entered are mandatory!
i tried Grid_SelectedIndexChanged1 but this not firing.. is there something i am missing or i need to change so that the evant is fired and i can update the other textbox??
I am new to ASP.NET so please help!
Thanks in advance!
If the updates are supposed to be triggered when the text changes, you should use the OnTextChanged event of the TextBox, and set AutoPostBack to true.
EDIT
To avoid duplicating efforts here, using the above approach you can find the row index using the technique that Pankaj Garg outlined in his answer:
int rowIndex = ((GridViewRow)((TextBox)sender).NamingContainer).RowIndex;
The biggest caveat to this approach is that it's not forgiving of changes in the markup. If you were to wrap the TextBox in another control that implements INamingContainer, the example above would break. For example:
<asp:TemplateField>
<asp:Panel ID="Panel1" runat="server"> <!-- becomes the naming container -->
<asp:TextBox ID="TextBox1" runat="server" onchange='valueChanged(<%# Container.ItemIndex %>);' />
</asp:Panel>
</asp:TemplateField>
That being said, you would want to notate your markup accordingly so other developers know to be careful when making changes.
EDIT
As an alternative, you could also trigger the postback in JavaScript using the onchange event of the TextBox:
<script type="text/javascript">
valueChanged = function(rowIndex){
__doPostBack("<%= GridView1.ClientID %>", rowIndex);
}
</script>
<asp:GridView ID="GridView1" runat="server" DataKeyNames="ID" ...>
<Columns>
<asp:TemplateField>
<asp:TextBox ID="TextBox1" runat="server" onchange='valueChanged(<%# Container.ItemIndex %>);' />
</asp:TemplateField>
</Columns>
</asp:GridView>
In the code-behind, override the RaisePostBackEvent method, and put your update logic there:
protected override void RaisePostBackEvent(IPostBackEventHandler source, string eventArgument)
{
base.RaisePostBackEvent(source, eventArgument);
if (source == GridView1)
{
int rowIndex = int.Parse(eventArgument);
TextBox txt = GridView1.Rows[rowIndex].FindControl("TextBox1") as TextBox;
if (txt != null)
{
var id = (int)GridView1.DataKeys[rowIndex]["ID"];
var text = txt.Text.Trim();
//update the database
}
}
}
You can check the Current Row Index like below...
((GridViewRow)((TextBox)sender).NamingContainer).RowIndex
Create a handler for OnTextChanged event and set the AutoPostBack Property True.
protected void TextBox_TextChanged(object sender, EventArgs e)
{
int CurrentGridIndex = ((GridViewRow)((TextBox)sender).NamingContainer).RowIndex
}
Related
I have a repeater with a textbox inside , and I want to fire an event
when I move from one textbox to another textbox , with the OnItemCommand of
the repeater .
<asp:Repeater ID="RptrPeople" runat="server" OnItemDataBound="RptrPeople_ItemDataBound" OnItemCommand="RptrPeople_ItemCommand">
<ItemTemplate>
<asp:HiddenField ID="hf" runat="server" Value="<%# Eval(this.ValuedPerson) %>" />
<asp:TextBox ID="txtDescription" runat="server" IsRequired="false" Visible="true" AutoPostBack="true" />
</ItemTemplate>
</asp:Repeater>
I tried to use the OnTextChanged of the Textbox , but I can't get the item that fired the event this way .
Can anyone please advise on a good way to get the item that fires the event , after I moved from one textbox , using the OnItemCommand (for example , I entered 123 in Textbox #1 , and then moved to Textbox #2 ... then I want to fire the event that takes care of the Textbox that has the 123 value) ?
Thanks
I tried to use the OnTextChanged of the Textbox , but I can't get the
item that fired the event this way .
The sender argument is always the control that triggered the event:
protected void txtDescription_TextChanged(Object sender, EventArgs e)
{
TextBox txtDescription = (TextBox) sender;
}
So you should use this instead of OnItemCommand because there the sender is the repeater.
If you also need to get the reference of the HiddenField use following code:
protected void txtDescription_TextChanged(Object sender, EventArgs e)
{
TextBox txtDescription = (TextBox) sender;
var item = (RepeaterItem) txtDescription.NamingContainer;
HiddenField hf = (HiddenField) item.FindControl("hf");
}
The NamingContainer of any control in a RepeaterItem is always the RepeaterItem. As an aside, that's working similar for other web-databound controls like GridView or DataList.
I wanto check the state of checkbox which is inside the data item template in devexpress grid view when the user click the ok button
here is my aspx code for gridviewColumn
<Columns>
<dx:GridViewDataColumn Caption="Data Source" FieldName="dataSrc" VisibleIndex="1"></dx:GridViewDataColumn>
<dx:GridViewDataColumn Caption="Download" FieldName="dwnloadConfig" VisibleIndex="2" Width="70px" >
<DataItemTemplate>
<dx:ASPxCheckBox ID="cbDwnloadConfig" ClientInstanceName="cbDwnloadConfig
" runat="server" >
</dx:ASPxCheckBox>
</DataItemTemplate>
</Columns>
Now when ok button is clicked i want to check the state of cbDwnloadConfig on server side
I used following code on cs file to access the dataitem template control but the checked state is always false.
ASPxCheckBox cbDwnload = gvDataSrc.FindRowCellTemplateControl(rwIndex[i], gvDataSrc.Columns["dwnloadConfig"] as GridViewDataColumn, "cbDwnloadConfig") as ASPxCheckBox;
So what is the proper way to check the state of checkbox which is inside the dataitemtemplate?
It may be that you are binding your grid view in PageLoad method and that may cause this behaviour.
You should bind your grid view in following way
if(!page.IsPostBack)
{
//BIND YOUR GRID
}
Above will prevent rebinding of gridview when button is clicked.
Well for those of you having the same issue i have found solution to the problem,
I had to use callback for this issue.
<Columns>
<dx:GridViewDataColumn Caption="Data Source" FieldName="dataSrc" VisibleIndex="1"></dx:GridViewDataColumn>
<dx:GridViewDataColumn Caption="Download" FieldName="dwnloadConfig" VisibleIndex="2" Width="70px" >
<DataItemTemplate>
<dx:ASPxCheckBox ID="cbDwnloadConfig" ClientInstanceName="cbDwnloadConfig" runat="server" >
<ClientSideEvents CheckedChanged="function(s,e)
{dwnloadSrc.PerformCallback(s.GetChecked());
}"></ClientSideEvents>
</dx:ASPxCheckBox>
</DataItemTemplate>
</Columns>
C#
protected void dwnloadSrc_OnCallback(object source, CallbackEventArgs e)
{
var param = e.Parameter;
//now check the paramater
//and do your magic
}
you also can pass two or multiple parameter at once from the front end/aspx
for that just do this:
dwnloadSrc.PerformCallback(s.GetChecked()+'_'+ gv.FocusedRowsIndex())
and at the Code behind you can check the parameter :
var param = e.Parameter;
var newParam = param.Split('_');
bool state = Convert.ToBoolean(newParam[0]);
int rwIndex = Convert.ToInt32(newParam[1]);
Thanks!
I am confused with this problem.
I have put a button in side the template field of a gridview and want to return the data from that specific GridView Row when that corresponding button is clicked.
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button2" CssClass ="btnSkin" runat="server" Text="Answer" Width="117px" onclick="Button2_Click" />
</ItemTemplate>
</asp:TemplateField>
In the button click event fireup, I want to read that data by creating a GridViewRow Element.
protected void Button2_Click(object sender, EventArgs e)
{
GridViewRow gvr = (GridViewRow)(sender as Control).Parent.Parent;
Label8.Text = gvr.Cells[1].Text;
Label10.Text = gvr.Cells[2].Text;
Label12.Text = gvr.Cells[3].Text;
}
Now the problem is, the GridViewRow Cells are returning empty strings.
What should I do?????
When using <asp:TemplateFields>, you actually need to find the text which is inside your controls such as <asp:Label> you used inside your <ItemTemplate>.
Cells won't have text, its the Controls inside the cells that have text.
So, If suppose , you have a Label inside one of your <ItemTemplate> as:
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("CustomerID") %>'>
</asp:Label>
</ItemTemplate>
Then access the Text of this Label control using below code in your button Click event:[ assuming, 2nd Column contains the above <ItemTemplate> defined ]
protected void Button2_Click(object sender, EventArgs e)
{
GridViewRow gvr = (GridViewRow)(sender as Control).Parent.Parent;
String str = ((Label)gvr.Cells[1].FindControl("Label1")).Text;
}
I found the reason behind the empty strings error.
Gridview.Cells[i].Text will return the string value only when it is a <asp:BoundField>
If it is a <asp:TemplateField> and you have some ASP control inside the <ItemTemplate>, you must follow the FindControl("<control_id>") approach.
Here, basically we look for the control object in that particular GridviewRow Cell by its ID and cast it into the corresponding Control Type. Now, we can use that as we call any other asp control from code-behind.
String str = ((Label)gvr.Cells[1].FindControl("Label1")).Text;
Try using GridView.RowCommand Event and refer the following link for the same
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx
hope this helps you.
Please check whether you are binding the grid properly on Page Load.
if(!IsPostBack)
{
BindgridView();
}
Hope this helps..Give a try..
I have two textbox and one gridview in my webform. The gridview is binded with database. But I want to add one more column on runtime which will be the textbox input from the webform. Because the scenario is like: I am maintaining two formula to calculate some percentage using the two textbox and the client wants to see this calculation for each row in the gridview.
But I cannot do this.
Is there anyone who can help me on this please? May be some suggestion.
Thanks in advance.
You could add the column in your GridView markup with a label control to display the result as follows.
Here is the markup needed, please note Visible is set to false.
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="label1" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Use the RowDataBound event to find the label and calculate your result as below:
void GridView1GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
//find the control
var label1 = e.Item.FindControl("label1") as Label;
if (label1 != null)
{
if (!string.IsNullOrEmpty(tbInput1.Text) && !string.IsNullOrEmpty(tbInput2.Text))
{
// Do the calculation and set the label
label1.Text = tbInput1.Text + tbInput2.Text;
// Make the column visible
GridView1.Columns[0].Visible = true;
}
}
}
}
Please forgive any errors, I have not tested the above.
I have a repeater that will present a set of titles and checkboxes (some checked some not). Each is wrapped in a div with a background colour. All I want to do is change the background colour for the checkboxes that are already checked so they are easily identified on the page.
Here's the repeater:
<asp:Repeater ID="rptCartridges" runat="server" OnItemDataBound="rp_ItemDataBound">
<ItemTemplate>
<div class="cartridgebox">
<span class="cartridgeboxl"><%#Eval("cartName") %></span>
<span class="cartridgeboxr">
<asp:CheckBox ID="chkCart" name="chkbox" Checked = '<%#Convert.ToBoolean(DataBinder.Eval(Container.DataItem, "cartChecked"))%>' runat="server" />
<asp:HiddenField ID="hfCartID" runat="server" Value='<%#DataBinder.Eval(Container.DataItem, "cartID")%>' />
</span>
</div>
</ItemTemplate>
</asp:Repeater>
All I really want to do is change the class cartridgebox to cartridgeboxchecked if the checkbox is returned as checked.
I have tried manipulating rp_ItemDataBound. Where it goes wrong is the actual changing of the class inline. I've tried using if statements, add runat="server" to the div and populating a variable and then using Response.Write inside the class statement. But nothing seems to work.
What seems the neatest way would be to use rp_ItemDataBound like so:
protected void rp_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
string chkboxClass = "cartridgebox";
CheckBox chk = (CheckBox)e.Item.FindControl("chkCart");
HiddenField hfCartID = (HiddenField)e.Item.FindControl("hfCartID");
// Adding the hide.Value Attribute to the chk.Text field.
chk.Attributes.Add("Text", hfCartID.Value);
if (chk.Checked == true)
{
chkboxClass = "cartridgeboxchecked";
}
else
{
chkboxClass = "cartridgebox";
}
}
But I lack the understanding to pass the variable chkboxClass to the div's class dynamically. Of course I am probably looking at this completely wrong so any guidance would be appreciated.
Use following markup for div in ItemTemplate : <div class='<%# ((bool)Eval("cartChecked"))? "cartridgeboxchecked" : "cartridgeboxl" %>' >
If you need to change div's class on checkbox change immediatelly, consider to add onclick client-side event handler to checkbox in ItemDataBound Repeater's event handler