I'm new to ASP.NET and have a form tag on an ascx user control. I'm unable to submit the form from javascript because I found out that this form is nested inside a form called 'aspnetForm'. If I just want to make a post to a cgi, how can I accomplish this?
Remove the <form runat='server'> if you don't need it and just use your own form: <form action="page.cgi" method="post">. You'll not be able to use some server controls. Use their HTML equivalents instead.
If you don't have control on the page, you can use javascript to inject a new form on the page with some hidden fields and set the values upon click of a button.
Something like this:
var myForm = document.createElement("form");
myForm.attributes["action"] = "mycgi.cgi";
myForm.attributes["method"] = "POST";
var myhiddenfield = document.createElement("input");
myhiddenfield.attributes["type"] = "hidden";
myhiddenfield.attributes["name"] = "name"
document.body.appendChild(myForm);
myForm.appendChild(myhiddenfield);
function onFormButtonClick() { // set as onclick on a <button>
myhiddenfield.value = ... //value read from a textbox or something.
...
myForm.submit();
}
Related
I have a main page: Main.aspx and 2 user controls User1.ascx and User2.ascx. First, i want User2.ascx to be invisible.I have a hidden value in the main page control. And if value of hidden value is not null then show user2.ascx. I have typed the code in the prerender function on user2.ascx.
Currently, what I try
In Main.aspx
<usercontrol:User1 runat="server" ID="user1control" Visible = "false" />
By this,
In User2, it comes only in pageload event but not in OnPreRender.
I have my all code in OnPreRender
Try something like this
protected void btnToggle_Click(object sender, EventArgs e)
{
string s = btnToggle.Text;
switch (s)
{
case "Hide":
btnToggle.Text = "Show";
break;
case "Show":
btnToggle.Text = "Hide";
break;
}
ucDetails myControl = (ucDetails)Page.LoadControl("~/ucDetails.ascx");
UserControlHolder.Controls.Add(myControl);
myControl.Visible = !myControl.Visible;
}
The other option you can create this on the javascript side. Where you can wrap your usercontrol in a panel and hide and show through javascript function. Hiding and showing through css.
<script type="text/javascript">
function hide() {
document.getElementById("ResultPanel2").style.display = 'none';
}
</script>
I Solved it. I kept a hidden field in main page, and in the load event of main page, I kept a contion that if the value is not null or empty then, user2.visible = true. This worked for me.
I am having two div tags and in it there are linkbuttons. When I click on any of the linkbutton I need to get the id of div tag in code behind. I have no idea how to do this.
Below is my code of one div tag:
<div id='qhse' class="qhse" runat="server"></div>
And C# code:
public void GetQHSEManual()
{
DataTable dsMenu = new DataTable();
gObj.category = "1";
dsMenu = gObj.GetAllSubCategory();
for (int i = 0; i < dsMenu.Rows.Count; i++)
{
LinkButton myLnkBtn = new LinkButton();
myLnkBtn.ID = "lbtn1" + dsMenu.Rows[i]["Subcategory"].ToString();
myLnkBtn.CssClass = "linkButton";
myLnkBtn.Click += new EventHandler(Dynamic_Click);
myLnkBtn.Text = dsMenu.Rows[i]["Subcategory"].ToString();
myLnkBtn.Text += "<br>";
qhse.Controls.Add(myLnkBtn);
}
}
$("a:contains('continue')").closest('div').attr('id');
Here in this code we are finding parent div id of a href having a text value in display as 'continue'.
You should do it with JavaScript. If you have 1 element inside of your div holder this should be done easy. JavaScript lib allows to use parent id when you click on your button inside div. See jQuery for this purpose.
I would suggest you that you separate server side and client side logic well. Think about local storage, sessions and cookies if you need to pass some data to the backend. In your case I would use an AJAX call from the client and implement some logic on the backend.
I've read a few articles regarding getting values back from a modal popup in an ASP .NET page, but they all seem to use JavaScript to accomplish this which isn't really want I want to do if possible.
I have a web user control which has a repeater that I populate from a list into a table. Each row has a link button which has a redirect url with a value as a query string.
Then I have another web user control which is just a link button with a panel and the repeater web user control that once clicked shows the actual modal popup.
Is it possible to get a value from the web user control once the link button on the repeater is clicked without having to redirect to the same page? I basically want to click on the link, show the modal and once closed, want to access the value.
I'm populating the repeater with the links as follows:
string linkUrl = "";
string action = "";
if (Request.QueryString["action"] != null)
{
action = Request.QueryString["action"];
switch (action)
{
case "SetupCompany":
{
linkUrl = "<a href=CreateCompanies.aspx?companyId=";
break;
}
case "ViewCompany":
{
linkUrl = "<a href=ViewCompany.aspx?companyId=";
break;
}
}
}
CompaniesBusinessManager mgr = new CompaniesBusinessManager();
var companies = mgr.GetCompanies(txtCompanyName.Text, txtRegistrationNumber.Text);
if (linkUrl != "")
{
foreach (var c in companies)
{
c.Name = linkUrl + c.Id.ToString() + "&action=" + action + ">" + c.Name + "</a>";
}
}
rptrCompanies.DataSource = companies;
rptrCompanies.DataBind();
if you don't want the page to be redirected, you will need to use javascript.
There is now way you can pass values from different controls without going back to the server.
In case you keep it without the javascript:
I think you need to pass values from one user control to another. I used to accomplish this by firing reachable events between them.
For example:
in your parent view:
<uc:YourUserControl runat="server" ID="UserControl_Test"
OnYourCustomAction="UserControl_YourUserControl_YourCustomAction" />
In your user control:
public event EventHandler<CustomActionEventArgs> YourCustomAction;
also in the same user control create a public trigger method to be access from others usercontrols
public void TriggerCustomActoinEvent(CustomActionEventArgs EventArgs)
{
if (this.YourCustomAction!= null)
{
this.YourCustomAction(this, EventArgs);
}
}
Hope this help, in on my way to home this was from my mind!
Without a page postback or JavaScript it not really possible. If you are using modal popups you are already using JS, so why not just get the value in JS? You could setup an event handler for all repeater buttons and if they are loaded via ajax use something like this to attach the event handler:
$(document).on('click', '.repeaterButton', function(e) {
var valueOfBtnClicked = $(this).val();
// Do something else
});
Is it possible to call "button click event" in one web form ,from a button in another web form?
what actually i'm trying to do is, i've a link button in second form, when it is clicked, i want the first form to disply and also button in first form to be clicked.
can anyone help me do this?
I assume you are using jQuery and have basic knowledge of it. So do it this way:
<form id="form1" style="display:none">
<Asp:Button id="button1" onclick="alert('clicked')" >button1</Asp:Button>
</form>
<form id="form2">
<Asp:LinkButton url="javascript:void(0);" onclick="call1();">Link button1</Asp:LinkButton>
</form>
<script>
function call1()
{
$("#form1").show();
$("#button1").trigger("click");
}
</script>
Note: The asp markup written by me can give compilation errors, so please resolve those yourself. I put that just to give you the basic idea of how it is handled.
create a Delegate in WebForms (I have used in WinForms)
public delegate void LoginDelegate(string s);
loginAirLineDelegate = new LoginDelegate(DisableToolStripMenuItems);
public void DisableToolStripMenuItems(string s)
{
this.viewToolStripMenuItem.Visible = true;
this.bookingToolStripMenuItem.Visible = true;
this.existingUserToolStripMenuItem.Visible = false;
this.newUserToolStripMenuItem.Visible = false;
this.toolStripStatusUserID.Text = "USerID :- "+s;
this.LoginUserId = s;
}
I have passed the Delaqgte to other Form with Construtor as argumnet.
I can able to fire the delegate from the second form like this
logDelegate(textBoxUserName.Text);
I need my MasterPage to be able to get ControlIDs of Controls on ContentPages, but I cannot
use <%= xxx.CLIENTID%> as it would return an error as the control(s) might not be loaded by the contentplaceholder.
Some controls have a so called BehaviourID, which is exactly what I would need as they can be directly accessed with the ID:
[Asp.net does always create unique IDs, thus modifies the ID I entered]
Unfortunately I need to access
e.g. ASP.NET Control with BehaviouraID="test"
....
document.getElementById("test")
if I were to use e.g. Label control with ID="asd"
....
document.getElementById('<%= asd.ClientID%>')
But if the Labelcontrol isn't present on the contentpage, I of course get an error on my masterpage.
I need a solution based on javascript. (server-side)
Thx :-)
You could use jQuery and access the controls via another attribute other than the ID of the control. e.g.
<asp:Label id="Label1" runat="server" bid="test" />
$('span[bid=test]')
The jQuery selector, will select the span tag with bid="test". (Label renders as span).
Best solution so far:
var HiddenButtonID = '<%= MainContent.FindControl("btnLoadGridview")!=null?
MainContent.FindControl("btnLoadGridview").ClientID:"" %>';
if (HiddenButtonID != "") {
var HiddenButton = document.getElementById(HiddenButtonID);
HiddenButton.click();
}
Where MainContent is the contentplace holder.
By http://forums.asp.net/members/sansan.aspx
You could write an json-object with all the control-ids which are present on the content-page and "register" that object in the global-scope of your page.
Some pseudo pseudo-code, because I can't test it at the moment...
void Page_Load(object sender,EventArgs e) {
System.Text.StringBuilder clientIDs = new System.Text.StringBuilder();
IEnumerator myEnumerator = Controls.GetEnumerator();
while(myEnumerator.MoveNext()) {
Control myControl = (Control) myEnumerator.Current;
clientIDs.AppendFormat("\t\"{0}\" : \"{1}\",\n", myControl.ID, myControl.ClientID);
}
page.ClientScript.RegisterStartupScript(page.GetType(),
"ClientId",
"window.ClientIDs = {" + clientIDs.ToString().Substring(0, clientIDs.ToString().Length - 2) + "};",
true);
}
It sounds like your issue is that you are using the master page for something it wasn't intended. The master page is a control just like any other control, and therefore cannot access any of the controls of its parent (the page). More info:
ASP.Net 2.0 - Master Pages: Tips, Tricks, and Traps
My suggestion is to inject the JavaScript from your page where the controls can actually be resolved. Here is a sample of how this can be done:
#Region " LoadJavaScript "
Private Sub LoadJavaScript()
Dim sb As New StringBuilder
'Build the JavaScript here...
sb.AppendFormat(" ctl = getObjectById('{0});", Me.asd.ClientID)
sb.AppendLine(" ctl.className = 'MyClass';")
'This line adds the javascript to the page including the script tags.
Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "MyName", sb.ToString, True)
'Alternatively, you can add the code directly to the header, but
'you will need to add your own script tags to the StringBuilder before
'running this line. This works even if the header is in a Master Page.
'Page.Header.Controls.Add(New LiteralControl(sb.ToString))
End Sub
#End Region