Showing data into Label OnTextChanged event of TextBox - c#

let say I enter a code into textbox 100 but into label Pizza should be texted in label
protected void Page_Load(object sender, EventArgs e)
{
}
protected void txtMain_TextChanged(object sender, EventArgs e)
{
lblmain.text = txtcat.text;
}

The OnTextChanged is a server side event . It would be better to use JavaScript for this purpose like this:
<script type="text/javascript">
function change() {
document.getElementById('<%= lblmain.ClientID %>').innerHTML = document.getElementById('<%= txtMain.ClientID %>').value;
}
</script>
And then in your TextBox call to change function like this:
<asp:TextBox ID="txtMain" runat="server" onkeydown="change();"></asp:TextBox>
<asp:Label ID="lblmain" runat="server" Text="Label"></asp:Label>

Related

onserverclick wont trigger the event

Nothing works, The button doesn't trigger the event
code of button:
<input type="button" id="regBtn" runat="server" onserverclick="RegClick"/>
script code:
<script runat="server">
void RegClick(object sender, EventArgs e)
{
regBtn.Value += "a";
}
</script>
If you use ASP.Net Web Form, you want to use Service Control whenever posting back to server. It will go through Life Cycle Events, and trigger the appropriate event correctly.
<asp:Button ID="SubmitButton" runat="server" Text="Submit" OnClick="SubmitButton_Click"/>
Code Behind
protected void Page_Load(object sender, EventArgs e)
{
}
protected void SubmitButton_Click(object sender, EventArgs e)
{
SubmitButton.Text += "a";
}

How to force a full postback on a button created in code behind and displayed in an UpdatePanel

I am using an asp.net UpdatePanel that contains Button 1. On Button1 click, a string is created in code behind then sent back to the updatePanel. That string contains Button2 that should fire a postback to execute Button2_Click function. Unfortunately I haven't been able to execute a full postback on button 2 yet.
If I add Button2 as a PostBackTrigger control in the UpdatePanel on my aspx page I get an error that the control (Button2) is not found. This makes sense cause Buton2 is created by clicking Button1 in code behind and is not available on page load.
I'm trying to RegisterPostBackControl(Button2) on page load but again I get an error that Button2 doesn't exist in the current context.
Any help would be highly appreciated.
ASPX page
<asp:ScriptManager ID="ScriptManager" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<fieldset>
<div runat="server" id="DIV1">Testing...<br /></div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" clientidmode="static"/>
<div runat="server" id="DIV2"></div>
<asp:Label ID="DIV2_LABEL" runat="server"></asp:Label>
</fieldset>
</ContentTemplate>
CODE BEHIND
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager.GetCurrent(Page).RegisterPostBackControl(Button2);
}
protected void Button1_Click(object sender, EventArgs e)
{
string DIV1_Html;
DIV1_Html= DIV1.InnerHtml;
DIV2.InnerHtml = DIV1_Html;
DIV2.InnerHtml += "<input id='Button2' type='button' value='button' runat='server' OnClick='Button2_Click' />";
}
protected void Button2_Click(object sender, EventArgs e)
{
DIV2_LABEL.Text = "Testing button2";
}
EDIT
Based on the advice below, I've added the button as follows:
Instead of:
DIV2.InnerHtml += "<input id='Button2' type='button' value='button' runat='server' OnClick='Button2_Click' />";
I've amended the code as follows:
Button Button2 = new Button();
Button2.Text = "Add";
Button2.Click += new EventHandler(Button2_Click);
PostBackTrigger pTrigger = new System.Web.UI.PostBackTrigger() { ControlID = Button2.ID };
UpdatePanel1.Triggers.Add(pTrigger);
DIV2.Controls.Add(Button2);
BUT I'm still missing something because Button2_Click function is not fired when I click on the new button with NO error. ?????
Since your Button2 is created in client-side, It's ID is not recognized by the updatePanel1.
You can create Button2 dynamically like this:
Button button2 = new Button();
PostBackTrigger pTrigger = new System.Web.UI.PostBackTrigger() { ControlID = button2.ID };
updatePanel1.Triggers.Add(pTrigger);
DIV2.Controls.Add(button2);
UPDATE: Here is a little sample that you can use:
public partial class WebForm1 : System.Web.UI.Page
{
private bool ShowButton2()
{
return ViewState["createButton"] == null ? false : (bool)ViewState["createButton"];
}
protected void Page_Load(object sender, EventArgs e)
{
if (ShowButton2())
CreateButton();
}
void CreateButton()
{
Button Button2 = new Button();
Button2.Text = "Add";
Button2.ID = "button2";
Button2.Click += Button2_Click;
PostBackTrigger pTrigger = new System.Web.UI.PostBackTrigger() { ControlID = Button2.ID };
updatePanel1.Triggers.Add(pTrigger);
DIV2.Controls.Add(Button2);
}
void Button2_Click(object sender, EventArgs e)
{
}
protected void button1_Click(object sender, EventArgs e)
{
if (!ShowButton2())
{
ViewState["createButton"] = true;
CreateButton();
}
}
}
The view state is a flag for showing Button2 of course.
There is a property on the button called OnClientClick, you can use that to call a javascript which then navigate to your page.
Example:
In your apsx page
<script type="javascript">
function reloadPage() {
window.location.href = '...';
}
In your code behind
DIV2.InnerHtml += "<input id='Button2' type='button' value='button' OnClick='ReloadPage()' />";

Calling a javascript method from the code behind

I need to call a JavaScript method with parameters from the code behind.
Javascript method
<script type="text/javascript">
function changeControlSample(path)
{
$find('<%= PartialUpdatePanel7.ClientID %>').set_UserControlPath(path);
$find('<%= PartialUpdatePanel7.ClientID %>').refresh();
}
</script>
<iucon:PartialUpdatePanel runat="server" ID="PartialUpdatePanel7"
DisplayLoadingAfter="500" InitialRenderBehaviour="Clientside" EncryptUserControlPath="false">
<LoadingTemplate>
<div style="margin-left: 84px; margin-top: 10px;">
<asp:Image ID="Image1" runat="server" ImageUrl="~/images/loading.gif" />
</div>
<div style="text-align: center">
Updating...
</div>
</LoadingTemplate>
</iucon:PartialUpdatePanel>
The code Behind of the page
protected Consultation controlconsultation = new Consultation();
protected void Page_Load(object sender, EventArgs e)
{
PartialUpdatePanel7.UserControlPath = "Espace_Candidat/Consultation.ascx";
controlconsultation.imageinfo += controlconsultation_imageinfo;
Session["controlconsultation"] = controlconsultation;
}
void controlconsultation_imageinfo(object sender, CommandEventArgs e)
{
PartialUpdatePanel7.UserControlPath = "Espace_Candidat/InfoEdition.ascx";
Page.ClientScript.RegisterStartupScript(this.GetType(),
"CallMyFunction",
"changeControlSample('Espace_Candidat/InfoEdition.ascx')", true);
}
Code behind of the user control
public event CommandEventHandler imageinfo ;
protected void Page_Load(object sender, EventArgs e)
{
Consultation current = (Consultation)Session["controlconsultation"];
imageinfo = current.imageinfo;
}
protected void Valider (object sender, CommandEventArgs e)
{
if (imageinfo != null)
{
string pageNumber = (string)e.CommandArgument;
CommandEventArgs args = new CommandEventArgs("Control", pageNumber);
imageinfo(this, args);
}
}
This call didn't work even I change the JavaScript method by another one.
For example, if I try
Page.ClientScript.RegisterStartupScript
(this.GetType(),
"CallMyFunction",
"alert('blabla');",
true);
I got the same result.
So, What is the error that I commited?
How can I fix my code?
If you have update panel in page then call like this,
ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), Guid.NewGuid().ToString(), #"<script type='text/javascript'>changeControlSample('" + path + "');</script>", false);
It don't have update panel then call like this
Page.ClientScript.RegisterStartupScript(this.GetType(), "tabselect", "<script type='text/javascript'>changeControlSample("' + path + '");</script>");
If you wish to call JavaScript method with parameters from the code behind you can get this done using
ClientScriptManager.RegisterStartupScript Method
please check the link given below:
http://msdn.microsoft.com/en-us/library/z9h4dk8y(v=vs.110).aspx
Hope this helps.

RequiredFieldValidator IsValid returning false on visible = false control

I want that when the textbox is visible = false, the RequiredFieldValidator shouldn't run.
This is my aspx code:
<asp:TextBox runat="server" ID="txtAmt" MaxLength="7" Style="width: 100px;"/>
<asp:RequiredFieldValidator ValidationGroup="ln" runat="server" ControlToValidate="txtAmt"
Display="Dynamic" ErrorMessage="Required" />
Now in my code behind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txtAmt.Visible = false;
}
}
and yet in my button click handler, when I do a Page.IsValid, it returns false if textbox is empty. Any idea how to solve this issue?
Just assign an ID to the validator and disable it.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txtAmt.Visible = false;
if(!txtAmt.visible) { txtamtValidator.Enabled=false};
}
}
using javascript you can achive this
<script type="text/javascript">
function txtAmtOff()
{
document.getElementById("txtAmt").style.display = 'none';
ValidatorEnable(document.getElementById("txtAmtValidator"), false);
}
function txtAmtOn()
{
document.getElementById("txtAmt").style.display = 'inline';
ValidatorEnable(document.getElementById("txtAmtValidator"), true);
}
</script>

How to get Text property of TextBox after ItemCommand event

I have a TextBox control inside a panel and this panel is inside DataList ItemTemplate.
After firing the ItemCommand event, everything works fine except that the TextBox.Text property is always an empty string "" although there is some text in it.
I tried several ways but without success. I would really appreciate if someone can assist me with this. Simplified code is shown below.
Thank you!
ASPX page:
<asp:DataList ID="dlDataList" runat="server" onitemcommand="dlDataList_ItemCommand">
<ItemTemplate>
<asp:Panel ID="pnlReply" runat="server" Visible="False">
<asp:TextBox ID="txtTextBox" runat="server"></asp:TextBox><br />
<asp:LinkButton ID="lnkbtnSend" CommandName="Send" runat="server">Send</asp:LinkButton>
</asp:Panel><br />
<asp:LinkButton ID="OpenPanel" CommandName="OpenPanel" runat="server">Open panel</asp:LinkButton>
</ItemTemplate>
</asp:DataList>
</asp:Content>
ASPX.CS Page code behind
protected void Page_Load(object sender, EventArgs e)
{
FillDataList();
}
private void FillDataList()
{
List<string> list = new List<string>();
list.Add("First");
list.Add("Second");
list.Add("Third");
dlDataList.DataSource = list;
dlDataList.DataBind();
}
protected void dlDataList_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "OpenPanel")
{
Panel pnlReply = (Panel)e.Item.FindControl("pnlReply");
pnlReply.Visible = true;
}
if (e.CommandName == "Send")
{
TextBox txtTextBox = (TextBox)e.Item.FindControl("txtTextBox");
//I tried this way also..
//TextBox txtTextBox = (TextBox)e.item.FindControl("pnlReady").FindControl("txtTextBox");
Label1.Text = txtTextBox.Text;
}
}
Please use IsPostBack in page load event. Without it your FillDataList(); is executing on every postback and resetting your DataList.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillDataList();
}
}

Categories

Resources