How to add textbox text to label and handling event handler - c#

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.

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 copy selectedtext from activecontrol textbox

i want to ask that how can i copy the selected text of active control textbox.
i tried this but it gives me the whole text , not the selected text :
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
string str = ActiveControl.Text;
}
and i didn't want to copy a text from a specific textbox , such as :
string str = textbox1.SelectedText;
i want to copy the Selected Text of any active control textbox with context Menu Strip
thank you.
TextBox activeTxtBox = ActiveControl as TextBox;
if(activeTxtBox!= null)
{
string str = activeTxtBox.SelectedText;
}

How to hide a text box with a dynamic ID from another controller event handler?

I have an AddRow() method which generates a check box and a text box for each row in the table, I need to control the text visibility by checking the check box in the same row.
that seemed to be easy for the first while, since I can get the targeted text box ID but I dont know how to do it now.
here is my method:
private void AddRow(int nRowIndex, string strPaymentStatus,string PaymentRemark)
{
// checkBox -----------------------------------------------------------------
CheckBox objCheckBox = new CheckBox();
objCheckBox.ID = strBillID;
objCheckBox.Checked = false;
objCheckBox.CheckedChanged += new EventHandler(cbxPaymentStatus_CheckedChanged);
objCheckBox.AutoPostBack = true;
// textBox----------------------------------------------------------
TextBox objTbxRemark = new TextBox();
objTbxRemark.ID = BillID;
objTbxRemark.AutoPostBack = true;
}
CheckBox handler:
protected void cbxPaymentStatus_CheckedChanged(object sender, EventArgs e)
{
// here I can get the BillID (textBox ID) by a query, but I need to control its visibility from here
}
any help will be appreciated..
You can try to use
Page.FindControl("id")
If you can reconstruct the correct id. They probably follow some kind of pattern based on the parents controls
The you have to cast to TextBox
TextBox txt = Page.FindControl("id") as TextBox ;
txt.Visible =...

Getting textbox values in ItemDataBinding or ItemDataBound event telerik report

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();
}

Retrieve the values from textbox which is dynamically created using C#

I have created few textbox dynamically while coding in the flow I have provided unique id for each and I have hard coded some values to all the text boxes using C#.
Now on click of button am trying to retrieve the values from the textbox for which I have used the below code, but its throwing an exception as OBJECT REFERENCE NOT SET TO INSTANCE OF AN OBJECT.
Please look at the below code, I have tried both the things but still am not getting. Please help me out.
Thanks
protected void btnPltGrap_onclick(object sender, EventArgs e)
{
//spny is my stack panel and txtX0 is my of the text box id
//Below is the 1st Try
TextBox tb = new TextBox();
tb= (TextBox)Master.FindControl("spnY").FindControl("txtX0");
string strx = tb.Text;
//Below is the 2nd Try
string strx = (spnY.FindControl("txtX0") as TextBox).Text;
}
Thanks
Am trying to use view state as per you told that i shlould recreate the controls ones again but am getting exception as Invalid Arguments. please go have a look.
protected void btnSet_onClick(object sender, EventArgs e)
{
Table tblMainY = new Table();
TableRow tblRow = new TableRow();
tblMainY.Controls.Add(tblRow);
TableCell tblCel = new TableCell();
TextBox txtdyn = new TextBox();
txtdyn.Text = "1";
txtdyn.ID = "txtY01";
txtdyn.Width = 50;
tblCel.Controls.Add(txtdyn);
tblRow.Controls.Add(tblCel);
splY.Controls.Add(tblMainY);
ViewState["temptbl"] = tblMainY
}
protected void btnPltGrap_onclick(object sender, EventArgs e)
{
splY.Controls.Add(ViewState["Temptbl"]);
}
Please help me out
I've had the same problem in the past.
What I did was give the dynamically-added control an ID, and made sure it retained that ID also on postback.
Once the postbacked control has the same ID as as before, Microsoft did magic and refilled the controls with the pre-postback values.
Read out this code once
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
this.NumberOfControls = 0; //very first time when page is loaded, value will be 0
else
this.createControls(); //if it is postback it will recreate the controls according to number of control has been created
}
//this is the base of this, it will hold the number of controls has been created, called properties
protected int NumberOfControls
{
get { return (int)ViewState["NumControls"]; }
set { ViewState["NumControls"] = value; }
}
//it will create the controls
protected void createControls()
{
int count = this.NumberOfControls;
for (int i = 0; i < count; i++) //loop for the total number of control.
{
TextBox tx = new TextBox(); //creating new control
tx.ID = "ControlID_" + i.ToString(); //in your solution you are giving static id, don't do that, assign id number dynamically, it will help you further, if you want to manipulate the controls for some other use
//Add the Controls to the container of your choice
form1.Controls.Add(tx);
}
}
//add new control
protected void addSomeControl()
{
TextBox tx = new TextBox();
tx.ID = "ControlID_" + NumberOfControls.ToString();
form1.Controls.Add(tx);
this.NumberOfControls++; //increment the number of control
}
protected void AddBtn_Click(object sender, EventArgs e)
{
addSomeControl();
}
Default.aspx
take placeholder tag in aspx file
< asp:PlaceHolder ID="PlaceHolder1" runat="server">
Default.aspx.cs
// adding/creating dynamic text box
TextBox txt = new TextBox();
txt.ID = "New_txt";
txt.TextMode = TextBoxMode.MultiLine;
txt.Text = dt.Rows[0]["message"].ToString();
txt.Width = 802;
txt.Height = 450;
txt.ReadOnly = true;
PlaceHolder1.Controls.Add(txt);
Retrive value from text box
string str = txt.Text;
some sample code in bellow link as my blog
Its explain for how to put and get textboxe's with values and validations in dynamicaly using panel control .
Let's go this url . and you can get good solutions
get and Create dynamic Textbox and dropdownlist with Validation
simple line for get textbox values in
TextBox objTextBox = (TextBox)PlaceHolder.FindControl("CorrecttextBoxName");
string value=objTextBox .Text;
You must recreate your controls on init to get it's value.
Here are some links
Get text from dynamically created textbox in asp.net
Edit 1
TextBox tb=(TextBox)ViewState["Temptbl"];
splY.Controls.Add(tb);

Categories

Resources