Asp Button is not binding on Event Handler - html

I'm using asp.net button control in my project but this button is unable to bind its Click event, ,I've written button code as
<asp:Button ID="btnProcess" runat="server" Text="Process" class="btn btn-default" OnClick="btnProcess_Click" />
and Code behind as this
protected void btnProcess_Click(object sender, EventArgs e)
{
MultiView1.ActiveViewIndex = 1;
MultiView1.Visible = true;
GridView.DataSource = Session["Table"];
GridView.DataBind();
}
I'm getting this error when I run that code
'ASP.fileuploadingmodule_aspx' does not contain a definition
for 'btnProcess_Click'
in fact on double clicking this button generates a javascript snippet instead of .cs event handler.
like this :
<script runat="server">
protected void btnProcess_Click(object sender, EventArgs e)
{
}
</script>
Project is made on asp.net 2.0 and visual studio 2012.
Any help will be appreciated.

Related

Unable to hide textbox when radio button checked

I am trying to hide/show text box when radio button is checked/unchecked using ASP.net but i am unable to do it. Can anybody help me out with this? what i am doing due to which i am unable to achieve what i want to.
Thats my asp code
protected void OnCheckChanged(object sender, EventArgs e)
{
lblCC.Visible = false;
txt_cardnum.Visible = false;
}
protected void OnCheckChanged1(object sender, EventArgs e)
{
lblCC.Visible = true;
txt_cardnum.Visible = true;
}
HTML
<asp:RadioButton ID="RadioButtonCC" runat="server" GroupName="PaymentMethod" Checked="true" CssClass="radio-inline" Text="Credit Card" OnCheckedChanged="OnCheckChanged1" />
<asp:RadioButton ID="RadioButtonEP" runat="server" GroupName="PaymentMethod" Text="Easy Paisa" CssClass="radio-inline" OnCheckedChanged ="OnCheckChanged" />
Did you change the AutoPostBack of both the radio controls to True ?
may be this can help you
Hide/Show JTextField

Getting a file from html input field with vaadin

I have to use a HTML snippet to get an image from Android/iOS devices with
<input type="file" accept="image/*" capture="camera" />
which is displayed in a Label:
Label label = new Label("<input type=\"file\" accept=\"image/*\" capture=\"camera\" />");
label.setContentMode(Label.CONTENT_XHTML);
Because I'm using Vaadin on Liferay I'm not sure how to obtain the Image since there is no POST submit
How am I able to get this image?
You need to write custom Vaadin component which extends FormPanel. Thats not hard as it may sounds, just let Vaadin Widget creator generate classes for you and then replace auto-generated GWT component with the following code
public class CameraCaptureWidget extends FormPanel {
public CameraCaptureWidget() {
setEncoding(FormPanel.ENCODING_MULTIPART);
setMethod(FormPanel.METHOD_POST);
FileUpload f = new FileUpload();
this.add(f);
f.getElement().setAttribute("accept", "image/*");
f.getElement().setAttribute("capture", "camera");
f.addChangeHandler(new ChangeHandler(){
#Override
public void onChange(ChangeEvent event)
{
submit();
}
});
}
}
You can also add button instead of adding ChangeHandler on FileUpload itself.
After you are done with that you need to handle submit() event in the Connector. Example:
public CameraCaptureConnector() {
getWidget().addSubmitHandler(new SubmitHandler()
{
#Override
public void onSubmit(SubmitEvent event)
{
Window.alert("Hello world");
}
});
}
You may also need to remove some unnecessary auto-generated methods.
This solution generates this HTML code in DOM:
<input capture="camera" accept="image/*" class="gwt-FileUpload" type="file">
Should you encounter trouble, please make sure you have read https://vaadin.com/wiki/-/wiki/Main/Creating+a+simple+component

how have a server side click of a div?

i have a div..inside the div i have an image and name..now when i will click on the div the div will have a server side click that means it will have a codebehind function..how to do it?? or how to make my div a button where i don't have to change my div...
my code
<div class="tutorial" style="margin-left:5px;">
TUTORIAL<div class="firstico" style="margin-left:70px;margin-top:-17px;">
</div>
</div>
how can i have a server side onclick function of this div???
i have used like that but it didn't work for me..don't know why??
<div class="tutorial" style="margin-left:5px;"
runat="server" id="tutorial" onClick="tutorial_Click">
TUTORIAL<div class="firstico" style="margin-left:70px;margin-top:-17px;">
</div>
</div>
private void tutorial_Click(object sender, System.EventArgs e){
// do stuff
}
i have tried like that also but it also didn't work for me...
<div class="tutorial" style="margin-left:5px;"
onclick="__doPostBack('tutorial', 'click');">
TUTORIAL<div class="firstico" style="margin-left:70px;margin-top:-17px;">
</div>
</div>
private void PageLoad(object sender, System.EventArgs e){
// sttufs
[...]
// my stuff for tutorial click
if (Request["__EVENTTARGET"] == "tutorial" && Request["__EVENTARGUMENT"] == "click"){
TutorialClicked();
}
}
private void TutorialClicked(){
iframestyle.Attributes["src"] = "userpage.aspx";
}
You will have to handle the click in the div using the Jquery and call server-side methods through JQuery.
Try it.
Source Code :-
<div class="tutorial" style="margin-left:5px;"
runat="server" id="tutorial" onClick="javascript:tutorial_Click('peram1')">click me
</div>
Javascript Code :-
<script type="text/javascript">
function tutorial_Click(parameter)
{
__doPostBack('tutorial', parameter)
}
</script>
Code :-
public void Page_Load(object sender, EventArgs e)
{
string parameter = Request["__EVENTARGUMENT"]; // parameter
// Request["__EVENTTARGET"]; // btnSave
if (parameter == "peram1")
TutorialClicked();
}
private void TutorialClicked(){
//write your code.
}
For more information refer __doPostBack
Lets say you want the page "tutorial.aspx" to be displayed.
Then make your div look like this:
<div class="tutorial" style="margin-left:5px;" onclick="location.href='tutorial.aspx';">
TUTORIAL
</div>
Lets say you have an iframe on your main page; (referring to this part of your code above)
private void TutorialClicked(){ iframestyle.Attributes["src"] = "userpage.aspx"; }
Wrap an anchor around a text like this
User Page
You can try this stuff-
Add a button to your html markup, and set it's style="display: none". so it won't be visible to anyone.
Now add a click event to this button.
When click on div call click event of the button
it may be like below
function tutorial_Click(){
// call here the button click which will do a postback
document.getElementById('<%= btn.ClientID %>').click();
}
4 . Do your code behind stuff at button click event at server side.
Hope this helps.
You can't listen for a click event from the server side. After the server has sent you the required files, the server is no longer involved.
You need a client side listener, jquery is very easy to use.
Index (or whatever html page u got):
<div id="clickme" class="tutorial" style="margin-left:5px;">
<div class="firstico" style="margin-left:70px;margin-top:-17px;">
TUTORIAL
</div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
$('#clickme').on('click', function () {//can replace '#clickme' for '.tutorial' if you want to be able to click multiple divs
//do stuff
$.ajax({//with ajax you can send a request to the server, and make it do somthing.
url: '/path/to/file',
type: 'post',
data: {variable: 'data'},
success: function(data) {//data is what you echo on from php
//do something on the site
alert(data);//just alert what you echo
}
});
});
</script>
And php (for example) a little but like this:
<?php
function doMe() {
//your function
}
if(isset($_POST['variable'])) {
if($_POST['variable'] === 'data') {
doMe();
}
}
?>

unable to get checked property of htmlinoutcheckbox on server side(code behind)

I have created a html input check-boxes dynamically from code behind and after rendering the check-boxes on to aspx page I'm unable to get the checked property of those check-boxes on button click event. week is the enum with alldays of a week. Here the sample code.
HtmlInputCheckBox chkbx = new HtmlInputCheckBox();
chkbx.Attributes.Add("id", ((week)i).ToString());
chkbx.Attributes.Add("runat", "server");
chkbx.Attributes.Add("name", ((week)i).ToString());
chkbx.Attributes.Add("value", "checked");
HtmlGenericControl label = new HtmlGenericControl("label");
label.Attributes.Add("for", ((week)i).ToString());
if (i == 1 || i == 7)
{
label.Attributes.Add("class", "dow disabled");
label.Attributes.Add("disabled", "true");
}
else
{
label.Attributes.Add("class", "dow");
chkbx.Checked = true;
}
label.InnerText = ((week)i).ToString().Substring(0,2);
_dowcontrol.Controls.Add(chkbx);
_dowcontrol.Controls.Add(label);
ASPX page
<body>
<form id="form1" runat="server" method = "post">
<t2:mycontrol ID="SampleControl" runat="server" >
</t2:mycontrol>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</form>
</body>
ASPX.CS page
What should be inside button click?
Tried
Request.Form["***"], FindControl("***")
In order for your dynamically created controls to interact with viewstate, events and other stages in the page life-cycle, they need to be created in the Init event. Creating these controls later in the life-cycle will exclude them from taking part in post value binding, viewstate binding, etc. Also note that you must recreate the controls on EVERY postback.
protected void Page_Init(object sender, EventArgs e)
{
// Do any dynamic control re/creation here.
}

Different behaviours for two different submit buttons?

I am trying to have two submit buttons in my form - one accepts meetings; the other declines them. They will both have different behaviours. How can I do is in my C# code?
The functionality I want is essentially
if(isPost) {
if(//accept button pressed(Request.Form[???]))
{
}
else
{
}
}
Here is my HTML :
<button name="accept" type="submit">Accept</button>
<div class="spacer"></div>
<button name="decline" type="submit">Decline</button>
<div class="spacer"></div>
Simple enough, but I cannot find a test for this on the Internet or in any documentation. Does anyone know what I would have for this ?
Give each button element the same name (in this example, 'SubmitButton') but a different value, then do a check for the different values in your server code, ie:
<button type="submit" name="SubmitButton" value="Accept">Accept</button>
<button type="submit" name="SubmitButton" value="Decline">Decline</button>
Then in your server code:
string buttonClicked = Request.Form["SubmitButton"]
if(buttonClicked == "Accept")
{
// Accept code
}
else if(buttonClicked == "Decline")
{
// Decline code
}
Be aware that this won't work on some earlier versions of IE though, and you may have to do a little javascript on the client prior to the post request being made.
As far as I remember, you have a controller action looking like this:
public ActionResult MyAction(string accept, string decline)
{
if (!string.IsNullOrEmpty(accept))
{
//do something
}
else
{
//do something else
}
}
Presuming you are using MVC3 you would do something like
#using (Html.BeginForm("Accept","Meeting"))
{
<input type="submit" value="Accept" />
}
#using (Html.BeginForm("Decline","Meeting"))
{
<input type="submit" value="Decline" />
}
You would then just have your accept and decline code in your Accept and Decline actions of your meeting controller respectively. No need for an if statement at all.
Example controller
public class MeetingController : Controller
{
[HttpPost]
public ActionResult Accept()
{
//Do accept stuff
return View();
}
[HttpPost]
public ActionResult Decline()
{
//Do decline stuff
return View();
}
}
Generally in the codebehind in c# you'd be looking for which button was clicked. You'd have an event handler for each button, and you code would react according to which button was clicked.
this.btnRejectAppointment.Click += new System.EventHandler(this.btnRejectAppointment_Click);
And then your method
private void btnRejectAppointment_Click(object sender, System.EventArgs e)
{
//code to reject appt.
}
And you'd have the SAME set for the other button.
ASP.NET Button control has OnClick event, so add different handler to every your button:
<asp:Button OnClick="MyHandler1" ... />
in code behind:
protected void MyHandler1(object sender, EventArgs args)
{ /* do stuff */ }
One of the possible solutions - use two buttons of type LinkButton but specify slightly different PostBackUrl:
<!-- Accept meeting button -->
<asp:LinkButton ID="acceptMeeting" runat="server"
PostBackUrl="MeetingsManager.aspx?Mode=Accept" />
<!-- Decline meeting button -->
<asp:LinkButton ID="declineMeeting" runat="server"
PostBackUrl="MeetingsManager.aspx?Mode=Decline" />
And in code behind:
public enum Mode
{
Unknown,
Accept,
Decline
}
protected void Page_Load(object sender, EventArgs args)
{
Mode currentMode = Mode.Unknown;
var rawMode = Request.QueryString["Mode"];
if (rawMode != null)
{
currentMode = (Mode)Enum.Parse(
typeof(Mode),
rawMode.ToString())
}
}