I have the next code and i don't get why it's not working.
<asp:Label ID="lblFull" Visible="false" runat="server">
<asp:TextBox ID="EmailtxtboxFull" Width="250px" runat="server" Font-Size="Medium" ForeColor="Blue" BorderWidth="1"></asp:TextBox>
</asp:Label>
<asp:Label ID="lblEnd" Visible="false" runat="server"></asp:Label>
<asp:Button ID="btnFull" OnClick="btnFull_Click" runat="server" Text="Send" Font-Size="Medium" ForeColor="#0066FF" BorderStyle="Solid" BorderWidth="1" />
c # code :
protected void btnFull_Click(object sender, EventArgs e)
{
String Email = EmailtxtboxFull.Text.ToString();
lblFull.Visible = false;
lblEnd.Visible = true;
lblEnd.Text = Email;
}
When i click on the btn ... i got empty lable and not the email that i wrote on the textbox. why this it's happent? that's connect to the autopostback ? how to fix that?
You have your EmailtxtboxFull textbox inside your label, are you sure the code sample is right?
when i corrected the label the code behaved correctly for me.
Related
Does anyone knows How to add ASP.NET control for <i></i> tags?
i am looking for ASP.NET Control that is equivalent with <i> Html tag
in my case, I also want to implement .FindControl() method for it inside the .cs file.
Here's the part of the code..
Example.axcs
<asp:Panel id="forDiv" CssClass="input-group colorpicker-component" runat="server">
<asp:TextBox ID="txtHeree" class="form-control" runat="server" Style="width: auto"></asp:TextBox>
<asp:Panel CssClass="input-group-append" runat="server">
<asp:Label CssClass="input-group-addon input-group-text" runat="server"><i></i></asp:Label>
</asp:Panel>
</asp:Panel>
from the code above,
the FindControl method that I have implemented goes like this,
Example.ascx.cs
TextBox txtVal = e.Row.FindControl("txtHeree") as TextBox;
Panel txtVal = e.Row.FindControl("forDiv") as Panel;
and, etc..
My current problem is, I still can't find both the equivalent ASP.NET Control for <i></i> tags and also for .FindControl() method for it.
sure, I have read the reference to another QnA forum from both Stackoverflow and Microsoft Docs:
ASP.NET Control to HTML tag equivalentandhttps://learn.microsoft.com/en-us/troubleshoot/developer/webapps/aspnet/development/server-controls
ANY suggestion, answer, or efficient code for the <i></i> tag control, will be very.. very helpful.
Thank You.
Sure, if you add a "id" tag, and runat=server, then that "i" thing becomes quite much a server side control.
And doing that allows use in listview, repeaters etc.
So, do this:
<i id="MyIguy" runat="server"></i>
<br />
<asp:Button ID="Button1" runat="server" Text="Button"
CssClass="btn" OnClick="Button1_Click" />
So, with code behind the button like this:
protected void Button1_Click(object sender, EventArgs e)
{
MyIguy.InnerText = "this is fun";
}
And we see this when run:
And say we use a gridview, repeater etc.?
say this markup, we just dropped in that (wiht id, and runat=server).
Quite much makes that control work, look, and feel like any other server side asp.net control.
Say, this markup and gridview (for the find control example).
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
DataKeyNames="ID" CssClass="table" >
<Columns>
<asp:TemplateField HeaderText="Hotel Name"> <ItemTemplate>
<asp:Label ID="txtHotel" runat="server" Text='<%# Eval("HotelName") %>' ></asp:Label>
</ItemTemplate> </asp:TemplateField>
<asp:TemplateField HeaderText="City"> <ItemTemplate>
<asp:Label ID="txtCity" runat="server" Text='<%# Eval("City") %>' ></asp:Label>
</ItemTemplate> </asp:TemplateField>
<asp:TemplateField HeaderText="Description"> <ItemTemplate>
<i id="Descript" runat="server"> <%# Eval("Description") %> </i>
</ItemTemplate> </asp:TemplateField>
<asp:TemplateField HeaderText="View"> <ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="View"
CssClass="btn" OnClick="Button1_Click" />
</ItemTemplate> </asp:TemplateField>
</Columns>
</asp:GridView>
Code to load:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = General.MyRst("SELECT * FROM tblHotelsA ORDER BY HotelName");
GridView1.DataBind();
}
}
And we have this now:
And our view button click code, say this:
protected void Button1_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
GridViewRow gRow = btn.NamingContainer as GridViewRow;
int? PKID = GridView1.DataKeys[gRow.RowIndex]["ID"] as int?;
Debug.Print("pk data base row id = " + PKID.ToString());
Debug.Print("Row click index = " + gRow.RowIndex);
// get hotel name
Label txtHotel = gRow.FindControl("txtHotel") as Label;
Debug.Print("Hotel name = " + txtHotel.Text);
// get our <i> guy
HtmlGenericControl myIthing = gRow.FindControl("Descript") as HtmlGenericControl;
Debug.Print("Our i guy = " + myIthing.InnerText);
}
Output :
so, most such controls - adding a "id" and runat="server", and at that point, it quite much behaves like most other asp.net controls.
//C# Code
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
TextBox2.Text = TextBox1.Text;
}
<h2 style="font-style: italic">
<asp:Label ID="Label5" runat="server" Text="Residental Address : "></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine"></asp:TextBox>
</h2>
<h2 style="font-style: italic">
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" oncheckedchanged="CheckBox1_CheckedChanged" Text="Both Same" />
</h2>
<h2 style="font-style: italic">
<asp:Label ID="Label9" runat="server" Text="Native Address :"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server" TextMode="MultiLine"></asp:TextBox>
</h2>
if residental address is same so on clicking on checkbox it must autopostback it to native address its but i tried and it showed blank
Did you follow the PageLoad event after clicking on checkbox, and see if textbox is getting reset somewhere in your code?
otherwise it should work.
I have a textbox which is inside a updatepanel -> detailsview. When I click insert I basically call a method to update DB. Now I'm trying to save the value typed in the textbox in the textbox so I don't lose it.
Shortly I wanna set the textbox value, with the value that is inserted.
My aspx:
<asp:UpdatePanel runat="server" ID="insert" UpdateMode="Conditional">
<ContentTemplate>
<table>
<tr>
<Fields>
<td class="style1">
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="500px" AutoGenerateRows="False"
DataKeyNames="strPositionId,nFolderId,tmVaRPosition" DataSourceID="ODSManualPosVaR"
OnItemInserted="DetailsView1_ItemInserted" OnItemInserting="DetailsView1_ItemInserting"
DefaultMode="Insert" SkinID="detailsviewSkin" EnableModelValidation="True">
<asp:TemplateField HeaderText="Name" SortExpression="strPositionName">
<InsertItemTemplate>
<asp:TextBox ID="strPositionName" Width="380px" MaxLength="49" runat="server" Text='<%# Bind("strPositionName") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Width="380px" Text='<%# Bind("strPositionName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</asp:DetailsView>
</td>
</Fields>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
My Page_Load:
protected new void Page_Load(object sender, EventArgs e)
{
base.Page_Load(sender, e);
strSystemId = Request["nSystemId"];
CheckPermission(strSystemId);
if (!IsPostBack || !PageHeader1.SystemId.Equals(strSystemId))
{
RefreshGrid();
DetailsView1.DataBind();
}
}
When click insert:
protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
{
UpdateDB();
//Trying to store the textbox value in a session variable
Session["Test"] = ((TextBox)DetailsView1.FindControl("strPositionName")).Text;
KeepValues();
}
KeepValues:
private void KeepValues()
{
var txtName = (TextBox)DetailsView1.FindControl("strPositionName");
var name = Session["Test"].ToString();
txtName.Text = name;
}
When I debug it stops at KeepValues and the Session variable is working. But I still can't set the textbox.text to it.
Why does this not work? Is it because of a postback? All I want is the value inside strPositionName to be stored in a variable (working) and then set as textbox.text
I have a nested repeater and I use a textbox in footer template. I wanto to get textbox.text value in button click. Here is my repeater:
<asp:Repeater ID="rprSSFirst" runat="server" OnItemDataBound="rprSSFirst_ItemDataBound" >
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
//******Some Items******
<asp:Repeater ID="rprSSNested" runat="server" > //Nested Repeater
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
//******Some Items******
</ItemTemplate>
<FooterTemplate>
<div style=" padding: 20px 35px;" id='ajax'>
<asp:TextBox ID="textbox" TextMode="MultiLine" Columns="50" Rows="10" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button2" runat="server" OnClick="btn_Save_Click" Text="Save" />
</div>
</FooterTemplate>
</asp:Repeater>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
//In Code Behind
protected void btn_Save_Click(object sender, EventArgs e)
{
TextBox txtAns = (TextBox)rprSSFirst.Controls[rprSSFirst.Controls.Count - 1].FindControl("textbox");
}
But txtAns Value is always null. How to get footer item textbox value in button click? or any other way?
Thanks for your answers.
You have to find the nested RepeaterItem first where both controls are sitting. You can get it by casting the NamingContainer:
protected void btn_Save_Click(object sender, EventArgs e)
{
Button btnSave = (Button) sender;
RepeaterItem item = (RepeaterItem) btnSave.NamingContainer;
TextBox txtAns = (TextBox) item.FindControl("textbox");
}
You can use Commandname property like this for button of nested repeater:
<asp:Repeater ID="rprSSNested" runat="server" OnItemCommand="rprSSNested_ItemCommand" >
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
//******Some Items******
</ItemTemplate>
<FooterTemplate>
<div style=" padding: 20px 35px;" id='ajax'>
<asp:TextBox ID="textbox" TextMode="MultiLine" Columns="50" Rows="10" runat="server" ></asp:TextBox>
<br />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Save" CommandName="cmd" CommandArgument="arg"/>
</div>
</FooterTemplate>
</asp:Repeater>
And add event in c# code like this:
protected void rprSSNested_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
if (e.CommandName == "cmd")
{
string ss = ((TextBox)e.Item.FindControl("textbox")).Text;
Response.Write(ss);
}
}
}
I am using datalist to show product id, name and a textbox("Qty") allow user to input order Qty. I got System.NullReferenceException: Object reference not set to an instance of an object error when user click an item to order. My datasource provides only 2 columns (product id and name). I added a textbox("Qty") and a button to the datalist. I can not get the value from the textbox("Qty") to submit. Could it be my datasource does not contain the "Qty" column thus FindControl alway return null value? How do I fix the problem? Thanks. Here is my code:
<asp:DataList ID="DataList1" runat="server" CellPadding="10" DataKeyField="product_id" DataSourceID="SqlDataSource1" RepeatColumns="2">
<ItemTemplate>
<asp:Label ID="product_id" runat="server"
Text='<%# Eval("product_id") %>' /><br/>
<asp:Label ID="product_name" runat="server"
Text='<%# Eval("product_name") %>' />
<br />
<asp:TextBox ID="Qty" runat="server"></asp:TextBox>
<asp:Button ID="ButtonAddToCart" runat="server" Text="Add to Cart" CommandName="addtocart2" OnCommand="DataList1_ItemCommand"
/>
</ItemTemplate>
</asp:DataList>
Here is the code for the button:
public void DataList1_ItemCommand(object source, System.Web.UI.WebControls.CommandEventArgs e)
{
var qtytxtbox = DataList1.FindControl("Qty") as TextBox;
// qtytxtbox always return null, why?
}
Your handler doesn't look correct. you should use DataListCommandEventArgs as second parameter. so try something like this
Markup:
<asp:DataList ID="DataList1" runat="server" OnItemCommand="DataList1_ItemCommand" vCellPadding="10" DataKeyField="product_id" DataSourceID="SqlDataSource1" RepeatColumns="2">
Then Add command name in button
<asp:Button ID="ButtonAddToCart" runat="server" Text="Add to Cart" CommandName="addtocart2" />
and code behind
public void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if(e.CommandName.Equals("addtocart2")
{
TextBox qtytxtbox = (TextBox)(e.Item.FindControl("Qty"));
}
}