Getting textbox values in ItemDataBinding or ItemDataBound event telerik report - c#

I am working on a telerik report. In my report there are three textboxes (textBox14, textBox15,textBox16).
In textBox14 and 15, showing a sum of values from data source based on sum group criteria. I want to show the sum of the textBox14 and 15 values in 16. How can I get the values of the textBox 14 and 15 in textBox16's ItemDataBinding or ItemDataBound event.
private void textBox16_ItemDataBound(object sender, EventArgs e)
{
string amt= textBox14.Value; // It getting only the mapped field name. ie Fields.amt, but I want the display text of the textBox14. There is no .Text property for textBox14.
string tax= textBox15.Value; // It getting only the mapped field name. ie Fields.tax, but I want the display text of the textBox14. There is no .Text property for textBox15.
}
I want to set the textBox16 value as
textBox16.Value = Convert.ToString(ConvetToInt32(amt) + ConvetToInt32(tax));
How can I get the values ?.

private void textBox16_ItemDataBound(object sender, EventArgs e)
{
string amt = textBox14.Text;
string tax = textBox15.Text;
}
and then call:
textBox16.Text = Convert.ToString(Convert.ToInt32(amt) + Convert.ToInt32(tax));

why don't you try the detail_ItemDataBound event
private void detail_ItemDataBound(object sender, EventArgs e)
{
Telerik.Reporting.Processing.DetailSection section = (sender as Telerik.Reporting.Processing.DetailSection);
Telerik.Reporting.Processing.TextBox = (Telerik.Reporting.Processing.TextBox)Telerik.Reporting.Processing.ElementTreeHelper.GetChildByName(section, "textBox14");
Telerik.Reporting.Processing.TextBox = (Telerik.Reporting.Processing.TextBox)Telerik.Reporting.Processing.ElementTreeHelper.GetChildByName(section, "textBox15");
Telerik.Reporting.Processing.TextBox txtTotal = (Telerik.Reporting.Processing.TextBox)Telerik.Reporting.Processing.ElementTreeHelper.GetChildByName(section, "textBox16");
txtTotal.Value = (Convert.ToInt32(txtAmt.Value) + Convert.ToInt32(txtTax.Value)).ToString();
}

Related

How to get value of TextBox of GridView in C#?

I'm little bit confusion, how to get value of TextBox of GridView in RowEditing and I've textchange event and that textbox value need to update via gridview to database. but before updating we need to calculate that value.
In RowUpdating we get value normally but in function calculationA() i'm not getting value of textbox. and need to calculate that value and show edited value in same textbox also.
public void calculationA()
{
TextBox txt_BCICU = (TextBox)grdlist.FindControl("txt_BCICU");
TextBox txt_BCSupDlx = (TextBox)grdlist.FindControl("txt_BCSupDlx");
txt_TotalChargeA.Text = (Convert.ToDecimal(txt_BCSupDlx.Text.Trim()) + Convert.ToDecimal(txt_BCICU.Text.Trim())).ToString();
protected void txt_BCICU_TextChanged(object sender, EventArgs e)
{
calculationA();
}
protected void grdlist_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txt_BCICU = (TextBox)grdlist.Rows[e.RowIndex].FindControl("txt_BCICU");
TextBox txt_BCSupDlx = (TextBox)grdlist.Rows[e.RowIndex].FindControl("txt_BCSupDlx");
}
APPROACH 1
You don't need TextChanged event to get value of textbox in gridview.
You can get the textbox value in RowUpdating event as in code below.
Also, remove the calculationA method and instead use the last line of code I have given in RowUpdating event.
protected void grdlist_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string textBox1Text = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_BCICU")).Text;
string textBox2Text = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_BCSupDlx")).Text;
//remove the calculationA function and just use the code below in
//RowUpdating event
txt_TotalChargeA.Text = (Convert.ToDecimal(textBox2Text.Trim()) + Convert.ToDecimal(textBox1Text.Trim())).ToString();
}
APPROACH 2
If you must have the TextChanged event then you can get textbox values as in code snippet below.
I have commented the call to calcualteA method since the same calculation can be done in TextChanged event. Note how the current grid row is obtained by getting the NamingContainer property of the textbox that raised the TextChanged event.
Get Textbox values in TextChanged event
protected void txt_BCICU_TextChanged(object sender, EventArgs e)
{
//find this textbox Text i.e. txt_BCICU Text
string txtBCICUText = (sender as TextBox).Text;
//find the current grid row and through it other textboxes text
GridViewRow currentRow = (sender as TextBox).NamingContainer as GridViewRow;
//find textbox txt_BCSupDlx Text
string txtBCSupDlxText = ((TextBox)currentRow.FindControl("txt_BCSupDlx")).Text;
//do your calculation here
txt_TotalChargeA.Text = (Convert.ToDecimal(txtBCSupDlxText.Trim()) + Convert.ToDecimal(txtBCICUText.Trim())).ToString();
//calculationA();
}

How to add textbox text to label and handling event handler

I have 4 text boxes and 3 different labels in different panels. my job is when i click any label that 4 text box texts should be assigned to respective label.
my code is as folows
string srlnumber;
string micr;
string accnumber;
string tc;
string compltedata;
private void lblMicr1_Click(object sender, EventArgs e)
{
srlnumber = txtSerialnumber.Text;
micr = txtMicr.Text;
accnumber = txtAccounno.Text;
tc = txtTC.Text;
compltedata = "C" + srlnumber +"C"+ micr +"A"+ accnumber +""+ tc;
lblMicr1.Text = compltedata;
}
my requirement is when i click label and then if i enter values to the text boxes it has to be assigned to the label. but it is not happening.
can anyone please help me
I assume that lblMicr1 is the label whose data you want to assign to
textbox1.
protected void lblMicr1_Click(object sender, EventArgs e)
{
lblMicr1.Text = compltedata
TextBox1.Text = lblMicr1.Text;
}
Or
this.MyTextBox.Text = this.MyLabel.Text;
use it , remember to declare label first and then textbox , so that
textbox can use label data.

Store ID of RowEditing Instead of Index

In my application, I give the user the option to search the database. When they click edit, I want the GridView to only show the row they are updating by storing its ID.
I'm trying the following code, but it is not returning anything other than the Control.
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
string value = GridView1.Rows[e.NewEditIndex].Cells[3].ToString();
}
Any ideas on how to store the value that I'm looking for?
Use the cell's Text property
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
string value = GridView1.Rows[e.NewEditIndex].Cells[3].Text;
// or
string value = GridView1.Rows[e.NewEditIndex].Cells[3].Value;
}
Or If you have declared the control as <TemplateField> for which you are trying to get the value for then try
string value = ""
Label lblID = (Label)GridView1.Rows[e.NewEditIndex].FindControl("lblID");
value = lblID.Text;
Another alternative would be to add DataKeyNames="ID" for the gridview which you can retrieve using
// assuming that the value of your datakey is numeric value
long Id = long.Parse(GridView1.DataKeys[e.NewEditIndex].Values["ID"].ToString());
Note you can add multiple comma separated values to DataKeyNames property.

Get index from several columns in listView

below I have some code to get the index from the selected row in a listView. But this only works if the user clicks on the first column. But if I have four columns, how would I do to let the user click anywhere on a row of the listView?
private void lvRegAnimals_SelectedIndexChanged(object sender, EventArgs e)
{
int index = lvRegAnimals.FocusedItem.Index;
// Code here to pass the index to method....
}
You have to set the ListView.FullRowSelect to true.
You could use ListView.SelectedItems property to do the work here,
Something like this
private void ListView1_SelectedIndexChanged_UsingItems(
object sender, System.EventArgs e)
{
ListView.SelectedListViewItemCollection breakfast =
this.ListView1.SelectedItems;
double price = 0.0;
foreach ( ListViewItem item in breakfast )
{
price += Double.Parse(item.SubItems[1].Text);
}
// Output the price to TextBox1.
TextBox1.Text = price.ToString();
}
For more info Go here

FormView Data Binding

I have for fields in my datasource that I want to combine and display in one label field. I have added a procedure to capture the databinding action but I don't how to get the data out of the datasource. I am displaying this information on a FormView is that makes anly difference. Can I get an example in c#?
For example -
protected void DisplayPayOut(object sender, EventArgs e)
{
Label Payout = FormView1.FindControl("PayoutLabel") as Label;
Payout.Text = datasource.field1 + datasource.field2;
}
I'm not fully sure but it seems like you're looking for something like the following:
protected void DisplayPayOut(object sender, EventArgs e)
{
Label Payout = FormView1.FindControl("PayoutLabel") as Label;
object dataItem = DataBinder.GetDataItem(FormView1);
Payout.Text = DataBinder.Eval(dataItem, "field1NameHere").ToString() + DataBinder.Eval(dataItem, "field2Namehere").ToString();
}

Categories

Resources