I had code to find label in gridview and I checked on it,s label text and it gives me the index not text and this error apear object refrence not set to ..... so I wnat to give CU.Username = LBL.Text; text no index of control
code
protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
LblRseult.Visible = true;
LblRseult.Text = "Successfully Process";
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
cUser CU = new cUser(this);
LBL = (Label)GridView1.Rows[e.RowIndex].Cells[1].FindControl("Label1");
CU.Username = LBL.Text;
if (CU.BasiclyExists())
{
LblRseult.Visible = true;
LblRseult.Text = "This user already exists";
}
}
You can access control inside TemplateField this way:
Label lbl = GridView1.Rows[e.RowIndex].FindControl("Label1") as Label;
Related
hello i want to redirect to another page if label in repeater value equals C# and the button is clicked.
here is my code
protected void Button1_Click(object sender, EventArgs e)
{
RepeaterItem item = (sender as Button).NamingContainer as RepeaterItem;
String name;
name = (item.FindControl("lblname") as Label).Text;
if (!IsPostBack)
{
if (name=="Csharp") { Response.Redirect("Csharp.aspx"); }
}
}
I add a label with its respective text to show dinamically using placeholder in ASP.NET coding C#, the next snippet is to show what I have for the time being
protected void Button1_Click(object sender, EventArgs e)
{
Label label1= new Label();
label1.ID="lbdin";
label1.Text="agregado dinamicamente";
TextBox textbox1 = new TextBox();
textbox1.Text = "textbox dinamico";
Button btn = new Button();
btn.ID = "btn";
btn.Text = "boton dinamico";
btn.Click += DynamicButton;
PlaceHolder1.Controls.Add(label1);
PlaceHolder1.Controls.Add(textbox1);
PlaceHolder1.Controls.Add(btn);
}
the controls appears dinamically in the placeholder, that works fine, my problem comes out when I try to retrive the text that the label control shows, in order to do that I've added a button and coded the next
protected void Button2_Click(object sender, EventArgs e)
{
Label Referencia_lb = PlaceHolder1.FindControl("lbdin") as Label;
//Label Referencia_lb = PlaceHolder1.FindControl("lbdin") as Label;
Referencia_lb.Text = "CAMBIANDO EL TEXTO DEL OBJETO CREADO EN TIEMPO DE EJECUCION";
}
but when debug the application I got the error
An exception of type 'System.NullReferenceException' occurred in WebApplication2.dll but was not handled in user code
could you please help me and tell me how to retrieve the text from the label that is created automatically into the placeholder
Replace the PlaceHolder1.FindControl("lbdin") as Label with:
var lbdin = PlaceHolder1.Children.Cast<Control>().FirstOrDefault(x => x.Id == "lbdin") as Label;
then you need to test for null.
if(lbdin != null)
{
lbdin.Text = "Your Text";
}
else
{ Response.Write("alert('could not find label');"); }
How do I get a value from the sender's children?
MouseUp on a Canvas creates a Grid.
private void ScrollViewer_MouseUp(object sender, MouseButtonEventArgs e)
{
Grid grid = new Grid();
Label timeLabel = new Label();
timeLabel.Content = "06:00"; //this could be anything
timeLabel.Name = "TimeStart"
grid.Children.Add(timeLabel);
canvas.Children.Add(grid);
grid.MouseDown += new MouseButtonEventHandler(ClickEvent);
}
When the user clicks on an already existing Grid, I want a MessageBox containing timeLabel.Content to appear, in this case, "06:00"
This is not working (I've tried some others as well, same result)
void ClickEvent(object sender, RoutedEventArgs e)
{
Grid test = (Grid)sender;
Label label = (Label)test.FindName("TimeStart");
MessageBox.Show(label.Content.ToString());
}
Error
An unhandled exception of type 'System.NullReferenceException' occurred in MissionControl M.exe
Additional information: Object reference not set to an instance of an object.
You Can use Registername for your label control and give a name, then access it using FindName
private void ScrollViewer_MouseUp(object sender, MouseButtonEventArgs e)
{
NameScope.SetNameScope(grid, new NameScope());
Label timeLabel = new Label();
timeLabel.Name = "label1";
grid.RegisterName("label1", timeLabel);
timeLabel.Content = "06:00";
}
void ClickEvent(object sender, RoutedEventArgs e)
{
Grid test = (Grid)sender;
if (test != null)
{
Label label = (Label)test.FindName("label1");
MessageBox.Show(label.Content.ToString());
}
}
You named your grid, yet you try to find your label by name. Pick one or the other. Probably, naming your label instead of your grid makes the most sense.
you should name you lable and then FindName
or you can use then first grid children :
Grid test = (Grid)sender;
if(test != null)
{
Label label = test.Children[0] as Lable;
MessageBox.Show(label.Content.ToString());
}
I am trying to access my dynamically created TextBox in C#, inside an event handler of a Button.
void MainFormLoad(object sender, EventArgs e)
{
this.Width=600;
this.Height=400;
this.FormBorderStyle= FormBorderStyle.FixedDialog;
TextBox t=new TextBox();
this.Controls.Add(t);
t.Location = new Point(60,40);
Label Mylable=new Label();
this.Controls.Add(Mylable);
Mylable.Location=new Point(15,43);
Mylable.Text="string : ";
t.Width=200;
t.Name="MyText";
t.Refresh();
Button Myb=new Button();
Myb.Location=new Point(270,40);
this.Controls.Add(Myb);
Myb.Text="Reverse it!";
Myb.Name="Mybo";
Myb.Click += new EventHandler(this.Myb_Clicked);
this.Refresh();
}
void Myb_Clicked(object sender, EventArgs e) {
// HOW SHOULD I GAIN ACCESS to MyText.Text HERE
MessageBox.Show();
}
Give a name to your dynamic TextBox:
TextBox t=new TextBox();
t.Name = "MyTextBox";
this.Controls.Add(t);
And then:
void Myb_Clicked(object sender, EventArgs e) {
string text = this.Controls["MyTextBox"].Text;
}
Wrong answer: object sender is the TextBox. You can cast sender to textbox and use it.
A decent way would be to make your textbox a class level member. And then you have access to it. If not, link TextBox.Text to a string property and use that.
You could keep a reference to your TextBox in your class
publc class MyForm: Form
{
TextBox myBox = null; // class member
void MainFormLoad(object sender, EventArgs e)
{
this.Width=600;
this.Height=400;
this.FormBorderStyle= FormBorderStyle.FixedDialog;
TextBox t=new TextBox();
myBox = t; // keep it for future reference
// rest of your code
}
void Myb_Clicked(object sender, EventArgs e) {
if (myBox !=null)
{
myBox.Text= "Clicked!";
}
MessageBox.Show();
}
}
I have a requirement to replace a label in gridview when I click on the Edit button added manually in grid view.
Please see below the code:
protected void btnchange_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
GridViewRow gvr = (GridViewRow)btn.NamingContainer;
int getindex = gvr.RowIndex;
gvorderdetail.Rows[getindex].Cells[3].Style.Value = "red";
gvorderdetail.Rows[getindex].Cells[0].Attributes["style"] = "background-color:Red";
gvorderdetail.Rows[getindex].Cells[1].Attributes["style"] = "background-color:Red";
gvorderdetail.Rows[getindex].Cells[2].Attributes["style"] = "background-color:Red";
TextBox tbx = (TextBox)gvorderdetail.Rows[getindex].FindControl("txtitemqty");
}
protected void gvorderdetail_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "change")
{
TextBox tbx = (TextBox)gvorderdetail.Rows[0].FindControl("txtitemqty");
}
}
Can anyone help?
'>
'>