Getting the ID of an HTML Control - c#

I have these in my aspx page:
<input runat="server" name="btnSubmit" id="btnSubmit" value="Submit" type="submit" OnServerClick="ibtn_Click" />
<input runat="server" name="btnCancel" id="btnCancel" value="Cancel" type="submit" OnServerClick="ibtn_Click" />
And in code behind inside the method "ibtn_Click" I added this line to get the id of a particular button:
HtmlGenericControl btn = sender as HtmlGenericControl;
But I got an error saying "Use the 'new' keyword to create an object instance."

Try this:
protected void ibtn_Click(object sender, EventArgs e)
{
HtmlInputButton input = (HtmlInputButton)sender as HtmlInputButton;
string ControlID = input.ID.ToString();
}

Related

Button Click doesn't work to make a H1 visible (asp.net)

I'm new at ASP.NET and web programming in general, here's my issue.
I have this DIV in my .aspx:
<div class="login">
<input type="text" placeholder="Usuário" name="usuario" id="usuario" runat="server"><br>
<input type="password" placeholder="Senha" name="senha" id="senha" runat="server"><br>
<button type="submit" class="btn-sucess" id="btn" runat="server" onserverclick="btn_Click" onclick="btn_Click">Login</button>
<h1 id="lbl1" runat="server" visible="false">Sucess</h1>
</div>
If the user clicks on the submit button it should be visible as it's written here in my aspx.cs
protected void btn_Click(object sender, EventArgs e)
{
lbl1.Visible = true;
}
Nothing happens. Why it doesn't work?
You're setting it to Visible false when rendering the control. Remove the visible property from aspx code and handle it in Page_Load event. All controls need to be inside form.
aspx
<form runat="server">
<div class="login">
<input type="text" placeholder="Usuário" name="usuario" id="usuario" runat="server"><br>
<input type="password" placeholder="Senha" name="senha" id="senha" runat="server"><br>
<button type="submit" class="btn-sucess" id="btn" runat="server" onserverclick="btn_Click" onclick="btn_Click">Login</button>
<h1 id="lbl1" runat="server">Sucess</h1>
</div>
</form>
Code-Behind
protected void Page_Load(object sender, EventArgs e)
{
lbl1.Visible = false;
}
With button click
protected void btn_Click(object sender, EventArgs e)
{
lbl1.Visible = true;
}
Also, if you want to handle anything at a client side, then alone use a javascript function btn_Click. But make sure that returns true and so the form will be submitted to the server.
Something like this,
Javascript
<script type="text/javascript">
function btn_Click()
{
return true;
}
</script>

Aspx form doesn't pass data to code behind file

I'm try to send the data in email field to the code and then work with it. However, the data never reaches the code, I've tried even setting breakpoints and it never halts at the breakpoint.
Form-
<form class="cd-form" runat="server" >
<label class="cd-label" for="email" runat="server">Console</label>
<input type="text" class="cd-email" name="email" id="email" runat="server" placeholder="Enter address to test connection">
<input type="submit" class="cd-submit" value="submit" runat="server" id="submit" onserverclick="Accept" />
<div class="cd-loading"></div>
</form>
Code behind file-
protected void Accept(object sender,EventArgs e)
{
String a = email.Value;
if( email.Value == "a#a.com" )
{
Response.Redirect("README.aspx");
}
}
Change
Email.value
To email.text
Change in your line
String a = email .Value ;
To
String a email.text;
protected void Accept(object sender,EventArgs e)
{
String a = email.text;
if( email.text== "a#a.com" )
{
Response.Redirect("README.aspx");
}
}

how to create button click event for form method?

I have Form tag in my layout page. I cant use form tags in content page, so I am using Form method="post" how to create button click event for this and how to give validation for html input type?
<form class="contact-form-title white" method="post">
<label id="lblFirstName" runat="server" title="First Name:">First Name:</label>
<input type="text" id="txtFirstNam" runat="server" /><br />
<label id="lblLastName" runat="server" title="Last Name:">Last Name:</label>
<input type="text" id="txtLastNam" runat="server" /><br />
<label id="lblEmail" runat="server" title="Email ID:">Email ID:</label>
<input type="text" id="txtEmail" runat="server" />
<label id="lblMessage" runat="server" title="Message">Message:</label>
<textarea id="txtMessag" runat="server" ></textarea><br />
<input type="submit" class="btn delicious f-center" runat="server" id="btnContac" name="SUBMIT" onserverclick="btnContac_Click" style="height:25%; width:10%;"/>
</form>
Back-End :
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnContac_Click(object sender, EventArgs e)
{
string FirstName = txtFirstNam.Value;
string LastName = txtLastNam.Value;
string EmailID = txtEmail.Value;
string Message = txtLastNam.Value;
}
Approach 1 : client side control
In Code behind use ispostback to catch the post back event,
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// write form population code;
}
else
{
//you will get posted data here;
}
}
to validate on client side, write your own java script validator something on below line.
<script type="text/javascript">
function validate()
{
var Firstname = document.getElementById('txtFirstNam').value
if (Firstname == "")
{
alert("Enter First Name");
return false;
}
if (LastName == "") {
alert("Enter Last Name");
return false;
}
}
</script>
then call this validator on submit button click
<input type="submit" class="btn delicious f-center" runat="server" id="btnContac" name="SUBMIT" onclick="return validate();" style="height:25%; width:10%;"/>
Option 2 : Server side control
Change all input and button into server controls (add runat=server) controls into server , then you can catch the event on server side in code behind like you are trying to do. good place to start
In server side approach, you have chance to use built in validators.
ASP.NET provides the following validation controls:
RequiredFieldValidator.
RangeValidator.
CompareValidator.
RegularExpressionValidator.
CustomValidator.
ValidationSummary.
for validator refer here

OnCheckedChanged not called after a postback

I am a new developer and I what I am trying to accomplish is hiding the radio buttons in the bottom DIV and the btnSetFinishState when clicked shows the bottom DIV (which it does from code behind). But the radio buttons when clicked do not fire the code behind method.
Here is .aspx:
<div class="row">
<asp:Button ID="btnSetFinishState" runat="server" Text="FINISHED" OnClick="btnSetFinishState_Click" UseSubmitBehavior="False" Enabled="False" />
</div>
<div class="row" id="finishedBlock" runat="server" visible="false">
<asp:Label ID="lblSetFinishText" runat="server" Text="Are you sure you want to set state to finished?" />
<div data-toggle="buttons" id="divRadioButtons">
<label id="btnFinishyes" class="btn btn-default col-sm-6 col-xs-6">
<asp:RadioButton ID="finishyes" Text="Yes" GroupName="finish" runat="server" AutoPostBack="true" OnCheckedChanged="finishyes_CheckedChanged" />
</label>
<label id="btnFinishno" class="btn btn-default col-sm-6 col-xs-6">
<asp:RadioButton ID="finishno" Text="No" GroupName="finish" runat="server" AutoPostBack="true" OnCheckedChanged="finishno_CheckedChanged" />
</label>
</div>
</div>
The aspx.cs code:
protected void btnSetFinishState_Click(object sender, EventArgs e)
{
// if neither radio button clicked
if (!finishyes.Checked && !finishno.Checked)
{
finishedBlock.Visible = true; // show the radio button DIV
//return;
}
}
protected void finishno_CheckedChanged(object sender, EventArgs e)
{
finishedBlock.Visible = false; // hide the radio buttons
finishno.Checked = false; // reset the no button to false
}
protected void finishyes_CheckedChanged(object sender, EventArgs e)
{
// set the finished state code
}
Is there e reason why btnSetFinishState is set to Enabled="False" ?
Because I tested this code and it works
aspx code
<div class="row">
<asp:Button ID="btnSetFinishState" runat="server" Text="FINISHED" OnClick="btnSetFinishState_Click" UseSubmitBehavior="False" Enabled="true" />
</div>
<div class="row" id="finishedBlock" runat="server" visible="false">
<asp:Label ID="lblSetFinishText" runat="server" Text="Are you sure you want to set state to finished?" />
<div data-toggle="buttons" id="divRadioButtons">
<label id="btnFinishyes" class="btn btn-default col-sm-6 col-xs-6">
<asp:RadioButton ID="finishyes" Text="Yes" GroupName="finish" runat="server" AutoPostBack="true" OnCheckedChanged="finishyes_CheckedChanged" />
</label>
<label id="btnFinishno" class="btn btn-default col-sm-6 col-xs-6">
<asp:RadioButton ID="finishno" Text="No" GroupName="finish" runat="server" AutoPostBack="true" OnCheckedChanged="finishno_CheckedChanged" />
</label>
</div>
</div>
aspx.cs
protected void btnSetFinishState_Click(object sender, EventArgs e)
{
// if neither radio button clicked
if (!finishyes.Checked && !finishno.Checked)
{
finishedBlock.Visible = true; // show the radio button DIV
//return;
}
}
protected void finishno_CheckedChanged(object sender, EventArgs e)
{
finishedBlock.Visible = false; // hide the radio buttons
finishno.Checked = false; // reset the no button to false
}
protected void finishyes_CheckedChanged(object sender, EventArgs e)
{
// set the finished state code
Response.Redirect("WebForm2.aspx");
}
I ran your code in my machine and found event's are firing exactly.
So , I think the different between your environment and mine may be <%# Page directive and missing AutoEventWireup="true" property.
Please share your Asp.net version and project info such as
is there any master pages or not.
UPDATE:
Please create a new sample web form application in visual studio and add a new web form say 'form1', paste your aspx and code behind to the newly added 'form1'. Test and if successfull then check the difference in aspx and code behind with your existing project.Also please let us know.

How do I return string from codebehind and display on web page?

in my HTML, I have a fileUpload button where you can choose a file to upload a file and then once you click pbcUploadBtn, it goes through validation in Validation.CheckWellFormed(...). The validation method returns an empty string if the file is fine or a string with the first error in the file otherwise. How can I have this display on my page using jQuery or js, etc.?
<div class="upload">
<asp:FileUpload ID="fileUpload" class="btn" runat="server" />
<asp:Button ID="pbcUploadBtn" class="btn btn-primary" runat="server" Text="Upload" onclick="uploadBtnClick" />
</div>
protected void uploadBtnClick(object sender, EventArgs e)
{
if (this.fileUpload.HasFile)
{
// file gets uploaded to szSaveToPath
Validation.CheckWellFormed(szSaveToPath);
//do things
}
}
You don't need to use js in this case.
You can add a label and change it's text property:
<div class="upload">
<asp:FileUpload ID="fileUpload" class="btn" runat="server" />
<asp:Button ID="pbcUploadBtn" class="btn btn-primary" runat="server" Text="Upload" onclick="uploadBtnClick" />
<asp:Label runat="server" ID="lblStatus"></asp:Label>
protected void uploadBtnClick(object sender, EventArgs e)
{
if (this.fileUpload.HasFile)
{
// file gets uploaded to szSaveToPath
lblStatus.Text = Validation.CheckWellFormed(szSaveToPath);
//do things
}
}

Categories