Storing the selected values in a hiddenfield inside a repeater - c#

I have a repeater and i have some controls like dropdown and text box how can i catch the drop down selected value and the value entered in the textbox and store them in the hidden field??
It is like a formula Left(c1+c2)*c3 the format is like this.

Repeater Code :
<asp:Repeater ID='myRepeater' runat="server" OnItemDataBound='myRepeater_OnItemDataBound'>
<ItemTemplate>
<asp:HiddenField ID='myHidden' runat="server" />
<asp:DropDownList ID="GeneralDDL" runat="server" AutoPostBack="True" />
</ItemTemplate>
</asp:Repeater>
**Code behind :**
protected void myRepeater_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
var myHidden = (HiddenField)e.Item.FindControl("myHidden");
foreach(RepeaterItem dataItem in 'myRepeater.Items)
{
myHidden.Value = ((DropDownList)'myRepeater.FindControl("GeneralDDL")).SelectedItem.Text;
// Same like for Text box
}
}

Related

C# ASP.NET How to insert Eval value Repeater from Code-Behind

In the repeater Design, I have something like this:
Text='<%# Eval("deposit") %>'
How do I insert the value from code-behind to "deposit" not using dataSource but by manually inserting string value?
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:Label ID="lblDeposit" runat="server" Text='<%# Eval("deposit") %>' />
</ItemTemplate>
</asp:Repeater>
You can use ItemDataBound event, to bind field values manually. Like:
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
Label lbl = e.Item.FindControl("lblDeposit") as Label;
lbl.Text = "You Manual String Value";
}
}
Attach this event to repeater in html markup:
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">

DropDownList in a GridView to bind the selected value of DropDown in TextBox inside a GridView in asp.net c#

i have a DropDownList in a GridView and want to bind the selected value of DropDown in TextBox inside a GridView to pass the selected value in query string in asp.net c#
please help me to guide and code of SelectedIndexChanged of DropDownList
screenshot of GridView
You could attach a method to the SelectedIndexChanged event on each child dropdown, either in the markup, either using a AddHandler in the RowDataBound event of your gridview
<asp:GridView ID="gv1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="ddl" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlSelectedIndexChanged">
<asp:ListItem Value="1"></asp:ListItem>
<asp:ListItem Value="2"></asp:ListItem>
<asp:ListItem Value="3"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:TextBox ID="txt1" runat="server" />
<asp:Button ID="btn1" runat="server" Text="redirect" />
Save the value wherever you want (hidden or textbox) when the selected item is changed
protected void ddlSelectedIndexChanged(object sender, System.EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
txt1.Text = ddl.SelectedValue;
}
And use this value when clicking the button
private void btn1_Click(object sender, EventArgs e)
{
Response.Redirect("mypage.aspx?q=" + txt1.Text);
}
or use jquery onChange event atttached to the dropdowns ?
$('select').on('change', function() {
alert( this.value );
})

How to get value from UserControl of Telerik RadGrid inside DataBound Event and Insert Event

I'm using telerik grid, here I've placed UserControl at RadGrid inside GridTemplateColumn something Like this
<telerik:GridTemplateColumn UniqueName="HardCoded" HeaderText="HardCoded" AllowFiltering="true"
DataField="AccountDesc">
<ItemTemplate>
<asp:Label ID="HardCoded" runat="server" Text='<%# Bind("HardCoded") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<UserCtrl:UserCtrl runat="server" ID="lbl" />
</EditItemTemplate>
<InsertItemTemplate>
<UserCtrl:UserCtrl runat="server" ID="lbl" />
</InsertItemTemplate>
</telerik:GridTemplateColumn>
here
here is my UserControl Combo Code
<telerik:RadComboBox runat="server" ID="HardCoded" >
<Items>
<telerik:RadComboBoxItem Value="1" Text="Member" Selected="true" />
<telerik:RadComboBoxItem Value="2" Text="Employee" />
<telerik:RadComboBoxItem Value="3" Text="All" />
</Items>
</telerik:RadComboBox>
inside databound Event I just want to set Selected Value if I press the Edit Button on grid. Here is my code of DataBound Event
protected void RadGrid1_OnItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
if (e.Item.IsInEditMode)
{
GridEditableItem editableItem = e.Item as GridEditableItem;
RadComboBox HardCoded = editableItem["HardCoded"].Controls[0] as RadComboBox;
// I've also tried
RadComboBox HardCoded = (RadComboBox)e.Item.FindControl("HardCoded");//it also didn't work
}
}
Here I always get the error of object reference not set to an instance of an object as It is unable to find My UserControl RadCombo
similarly if I want to get value inside of Insert event I'm unable to find my combo
here is my code
protected void RadGrid1_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
string HardCoded = ((RadComboBox)ii["HardCoded"].FindControl("HardCoded")).SelectedValue;
}
similar exception of object reference not set
Instead of using GridEditableItem in the OnItemDataBound event handler, use GridEditFormItem as shown below:
protected void RadGrid1_OnItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridEditFormItem && e.Item.IsInEditMode)//the grid is about to Edit.
{
GridEditFormItem item = (GridEditFormItem)e.Item;
RadComboBox combo = (RadComboBox)item.FindControl("HardCoded");
combo.SelectedValue = "Something";
}
}

How to get GridView checkAll checkbox to not fire the individual checkboxes?

I have a GridView with each row containing a checkbox, and the header of that column is a checkbox with checkAll functionality:
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<input id="chkAll" onclick="javascript:SelectAllCheckboxes(this);" runat="server" type="checkbox" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" ToolTip="Select this order" AutoPostBack="true" OnCheckedChanged="chkSelect_OnCheckedChanged"/>
</ItemTemplate>
</asp:TemplateField>
...More BoundFields
</Columns>
The javascript behind the SelectAllCheckboxes()
function SelectAllCheckboxes(spanChk) {
// Added as ASPX uses SPAN for checkbox
var oItem = spanChk.children;
var theBox = (spanChk.type == "checkbox") ?
spanChk : spanChk.children.item[0];
xState = theBox.checked;
elm = theBox.form.elements;
for (i = 0; i < elm.length; i++)
if (elm[i].type == "checkbox" &&
elm[i].id != theBox.id) {
//elm[i].click();
if (elm[i].checked != xState)
elm[i].click();
//elm[i].checked=xState;
}
}
My GridView basically contains sales (orders) from my website, so dollar amounts. The OnCheckedChanged event adds (if checked) or subtracts (if unchecked) the current row's price from a totalAmount that is displayed on the page.
This all works great except that when I click the checkAll checkbox, all of the OnCheckedChanged events for all of the row check boxes fire and it takes a long time to process it all. Since all that I am doing in the OnCheckedChanged method is summing amounts, is there a way that I can NOT call the events for the individual checkboxes and just make one call to a separate method that will grab all the GridView rows and sum them all at once?
If you consider doing it all on the server in one postback, you could try this:
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" ToolTip="Select this order" AutoPostBack="true" OnCheckedChanged="chkAll_OnCheckedChanged"/>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" ToolTip="Select this order" AutoPostBack="true" OnCheckedChanged="chkSelect_OnCheckedChanged"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
On the code behind:
protected void chkSelect_OnCheckedChanged(object sender, EventArgs e)
{
//Process checked item
}
protected void chkAll_OnCheckedChanged(object sender, EventArgs e)
{
foreach (GridViewRow item in grdTest.Rows)
{
CheckBox ckb = (CheckBox)item.FindControl("chkSelect");
//This will not call the individual event
ckb.Checked = ((CheckBox)sender).Checked;
//Process checked item
}
}

Can I access objects (button or hyperlink) within a templatefield in a Gridview?

I want to access an object (hyperlink or button) within a templatefield in a gridview.
How do I do this?
Assuming that you're binding data to it, you'll want to look at doing that inside the RowDataBound event.
Here's an example of how to retrieve a control within a Template Field:
.aspx:
<asp:GridView ID="GridView1" Runat="server"
OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Template Field">
<ItemTemplate>
<asp:Button ID="btnTest" Runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code-behind:
void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button btnTest = (Button)e.Row.FindControl("btnTest");
btnTest.Text = "I'm in a Template Field";
}
}
You can use it on RowDataBound or click event of your template control like
TextBox txtTemp= (TextBox )e.Row[e.RowIndex].FindControl("yourControlName");
string someText=txtTemp.Text;

Categories

Resources