I am trying to create a textbox dynamically. I can successfully create it via the below code:
var parentTable = $('<table border="0" class="table table-bordered" width="100%" id="searchInformation"/>');
var tr = $("<tr class='searchEntitydata'/>");
var tr2 = $("<tr class='searchEntitydata1'/>");
tr.append("<td id='HQNametd'> <label ID='hqName'>HQ Name</label> <input type='textBox' id='textboxSearch' placeholder='Search HeadQuarter'> </td>");
tr.append("<td id='HQIDtd'> <label ID='hqId'>HQ ID</label> <input type='textBox' id='textboxSearchHQId' placeholder='Search ID'> </td>");
tr.append("<td id='BranchNametd'> <label ID='BranchName'>Branch Name</label> <input type='textBox' id='textboxSearchBranchName' placeholder='Search Branch Name'> </td>");
tr.append("<td id='BranchIdtd'> <label ID='BranchId'>Branch Id</label> <input type='textBox' id='textboxSearchBranchId' placeholder='Search Branch ID'> </td>");
tr2.append("<td id='Citytd'> <label ID='City'>City</label> <input type='textBox' id='textboxSearchBranchId' placeholder='Search Branch ID'> </td>");
tr2.append("<td id='Statetd'> <label ID='State'>State</label> <input type='textBox' id='textboxSearchBranchId' placeholder='Search Branch ID'> </td>");
parentTable.append(tr);
parentTable.append(tr2);
$('#confirmSearch').append(parentTable);
However, when I change the textbox type from input type=textBox
to asp:TextBox for using runat='server', I get an error.
Is there any way via which I can dynamically create a textbox with runat=server?
NO you cannot add asp-runat="server" tag from jQuery or any client side script. Rather use following to generate textbox dynamically from server side, encapsulate your code within ajax control toolkit update panel.
C#: (On click or page load or whatever server side event)
TextBox tbOne = new TextBox();
Label lblOne = new Label();
lblOne.ID = "lblId";
lblOne.Text = "My label";
lblOne.Width = 200;
tbOne.ID = "tbId";
tbOne.Text = "xyz";
pnl.Controls.Add(lblOne);
pnl.Controls.Add(tbOne);
ASPX:
<asp:Panel ID="pnl"Width="100%" runat="server">
</asp:Panel>
Related
I'm facing issue and selection dropdown value from html form in selenium C#
I tried a lot but everytime I'm getting error of "unable to find element"
HTML Form
<div class="ui-store-assign-role ng-scope">
<div class="ui-assign-label">
<h3>Assign Role</h3>
<span ng-model="vm.viewModel.restrictToBol" ng-click="vm.restrictSellerPermissions(!vm.viewModel.restrictToBol,1)" class="ng-pristine ng-untouched ng-valid"><!-- ngIf: vm.viewModel.restrictToBol --><span ng-if="vm.viewModel.restrictToBol" class="ng-scope">Release from</span><!-- end ngIf: vm.viewModel.restrictToBol --><!-- ngIf: !vm.viewModel.restrictToBol --> BOL</span>
</div>
<div class="ui-assign-role-tags">
<!-- ngRepeat: customerRole in vm.viewModel.userRoles --><div ng-repeat="customerRole in vm.viewModel.userRoles" class="ng-scope">
<span class="ng-binding">Wholesalers <em class="fa fa-close" data-toggle="modal" data-target="#deleteCustomerRole" data-ng-click="vm.ShowDeleteConfirmationForCustomerRole(customerRole.Id,customerRole.RoleType)"></em></span>
</div><!-- end ngRepeat: customerRole in vm.viewModel.userRoles -->
</div>
<select class="form-control ng-pristine ng-valid ng-touched" name="userRoles" ng-model="vm.viewModel.selectedRoleType" ng-options="role for role in vm.viewModel.roleTypes" ng-change="vm.addCustomerRole()"><option value="" class="" selected="selected">--- Select customer role ---</option><option label="Affiliate" value="string:Affiliate">Affiliate</option><option label="Designer" value="string:Designer">Designer</option><option label="ContestAdmin" value="string:ContestAdmin">ContestAdmin</option><option label="DesignAdmin" value="string:DesignAdmin">DesignAdmin</option><option label="Seller" value="string:Seller">Seller</option><option label="TemplateSeller" value="string:TemplateSeller">TemplateSeller</option></select>
</div>
Element Tried:
dropdown_CustomerRole = //*[#name='userRoles']
Driver.SetDropdownValue(dropdown_CustomerRole, "Seller");
firstly you need to import OpenQA.Selenium.Support.UI and after that here is how selects/dropdowns should be handled
// select the drop down list
// by xpath
IWebElement education = driver.FindElement(By.XPath("//*[#name='userRoles']"));
// by css
IWebElement education = driver.FindElement(By.CssSelector("select[name='userRoles']"));
//create select element object
SelectElement selectElement = new SelectElement(education);
//select by value
selectElement.SelectByValue("Seller");
// select by text
selectElement.SelectByText("Seller");
My problem i always get null from my inputs or default value. Some how if i set value at page_load like Form_txt_Ad.Value="ExampleValue"; i can get it. But i cant get any value from inputs.
protected void Save_Button_Click(object sender, EventArgs e)
{
string exapmle = Form_txt_Ad.Value;
string example = Form_txt_Soyad.Value;4
}
<div class="input">
<input type="text" translate translate-attr-placeholder=".PLACEHOLDER_NAME" placeholder="Ad" id="Form_txt_Ad" runat="server" />
<span><i class="glyphicon glyphicon-user"></i></span>
</div>
<div class="col-md-12" style="text-align: center;">
<button type="button" runat="server" onserverclick="Save_Button_Click" class="btn btn-success btn-raised btn-lg" title="Kaydet"><i class="glyphicon glyphicon-floppy-saved icon-marginRight"></i>Kaydet</button>
</div>
Thx for help.
Make sure all your control elements are placed inside <form> ... </form> tag.
Since you have placed runat="server" you should be able to get the value by either using any of them
Form_txt_Ad.Value
(OR)
Form_txt_Ad.Text
Else use Request.Form["Form_txt_Ad"]
Not sure though why not use a server side control using <asp:TextBox ... which will allow you to get the textbox value directly using the Text property
Add the name attribute to your input and make sure it's inside a form element.
<form>
...
<input type="text" translate translate-attr-placeholder=".PLACEHOLDER_NAME" placeholder="Ad" id="Form_txt_Ad" name="Form_txt_Ad" runat="server" />
...
</form>
I am trying to implement Sign Up api of Constant Contact. They gave me this HTML code. Runs properly if i save it as a HTML page.
What i want to do is convert this form to C# code that will execute onClick of a button. The HTML portion is looking fine but today is my first day with ASP.net and i have no idea what code to put in the code-behind .cs file. Searched a lot and am super confused.
This method is clicked when the submit button is clicked:
protected void buttonId_Click(object sender, EventArgs e)
{
}
Here is my ui side code:
<asp:TextBox ID="TextBox3" runat="server" class="name" value="First Name" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'First Name';}"></asp:TextBox>
<asp:TextBox ID="TextBox4" runat="server" class="name" value="Last Name" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Last Name';}"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" class="name" value="Join our mailing list" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Join our mailing list';}"></asp:TextBox><br>
<asp:Button id="buttonId" OnClick="buttonId_Click" class="btn btn-info sub1" runat="server" Text="SUBSCRIBE">
</asp:Button>
Here is the HTML code that i got from constant contact:
<form data-id="embedded_signup:form" class="ctct-custom-form Form" name="embedded_signup" method="POST" action="https://visitor2.constantcontact.com/api/signup">
<p>Sign up to get interesting news and updates delivered to your inbox.</p>
<!-- The following code must be included to ensure your sign-up form works properly. -->
<input data-id="ca:input" type="hidden" name="ca" value="my-secrect-key">
<input data-id="list:input" type="hidden" name="list" value="3">
<input data-id="source:input" type="hidden" name="source" value="EFD">
<input data-id="required:input" type="hidden" name="required" value="list,email">
<input data-id="url:input" type="hidden" name="url" value="">
<p data-id="Email Address:p" ><input data-id="Email Address:input" type="text" name="email" value="" maxlength="80"></p>
<p data-id="First Name:p" ><input data-id="First Name:input" type="text" name="first_name" value="" maxlength="50"></p>
<p data-id="Last Name:p" ><input data-id="Last Name:input" type="text" name="last_name" value="" maxlength="50"></p>
<button type="submit" class="Button ctct-button Button--block Button-secondary" data-enabled="enabled">Sign Up</button>
</form>
Here's an example of some code you could put in your button OnClick event. This POST's data to another URL.
Hopefully you can figure out what's going on in this code. Basically it is building a string (data) with all of the data from the HTML form and submitting this to the other website using HTTP POST.
Most likely you would also need to check the response back from the other website.
string remoteUrl = "https://visitor2.constantcontact.com/api/signup";
ASCIIEncoding encoding = new ASCIIEncoding();
string data = "ca=my-secrect-key&list=3&source=EFD&required=list,email&url=&email=" + Server.UrlEncode(TextBox1.Text) + "&first_name=" + Server.UrlEncode(TextBox2.Text) + "&last_name=" + Server.UrlEncode(TextBox3.Text);
byte[] bytes = encoding.GetBytes(data);
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(remoteUrl);
httpRequest.Method = "POST";
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.ContentLength = bytes.Length;
using (Stream stream = httpRequest.GetRequestStream())
{
stream.Write(bytes, 0, bytes.Length);
stream.Close();
}
I hope you can Help me with this.
I'm doing an Automation script in VS 2012 with C# and I need change the value of a Property so I'm doing this
HtmlLabel htmlLabel = new HtmlLabel(tableDocuments);
htmlLabel.FilterProperties[HtmlLabel.PropertyNames.TagInstance] = taginstanceCount.ToString();
UITestControlCollection lblCollection = htmlLabel.FindMatchingControls();
HtmlLabel desiredLabel = (HtmlLabel)lblCollection[0];
But when I Watch in VS the
desiredLabel.TagInstance the value is 9 and the value of the taginstanceCount.ToString() is 7, So when the script try to do click in the document that I indicate that is the document with TagInstace = 7 isn't is doing click in the taginstanceCount.ToString(). I hope you can help me with this problem.
html code behind
<span class="field"> <label class="checkbox document-check" style="margin-right: 10px; margin-left: 10px;" for="check2">
<input name="checkbox[]" id="documentID" type="checkbox" value="2" data-internalid="161">
<span></span>
</label> </span>
This is the declaration of the tableDocument
HtmlTable tableDocuments = this.UIAllianceUnitedInternWindow.UIAllianceUnitedDocument.UIIFramedataFrame2.UIAllianceUnitedDocument.UIDocumentsTabPane.tableDocuments;
Regards.
I'm having problems with a chunk of code that's meant to add a textbox to a Repeater in ASP.
I have the following:
<asp:Repeater ID="uxRolesList" runat="server">
<ItemTemplate>
<div id="<%# GetRolesDivId() %>" class="div_row">
<asp:TextBox ID="uxTxtBoxRole" runat="server" rows="5" columns="100" Text='<%# DataBinder.Eval(Container.DataItem, "RequirementDescription") %>' TextMode="multiline" MaxLength="2000"></asp:TextBox>
<input type="button" style="vertical-align:top;" value="X" class="remove-roles-btn" />
<br /><br />
</div>
</ItemTemplate>
</asp:Repeater>
Which generates a load of textboxes that look like this in the html:
<td id="rolesColumn">
<div id="roles-0" class="div_row">
<textarea name="ctl00$mainContent$uxRolesList$ctl00$uxTxtBoxRole" rows="5" cols="100" id="ctl00_mainContent_uxRolesList_ctl00_uxTxtBoxRole">Cool Job1</textarea>
<input type="button" style="vertical-align:top;" value="X" class="remove-roles-btn" />
<br /><br />
</div>
</td>
I've also added the following button, that should add a textbox to this list when hit:
<asp:Button CssClass="btn" ID="uxAddRoleBtn" runat="server" Text="Add a new role requirement" />
Using the following jQuery code:
$("#ctl00_mainContent_uxAddRoleBtn").live("click", (function (e) {
var rolesCounter = $('#ctl00_mainContent_uxTxtBoxRolesCount').val();
if (rolesCounter < 10) {
var rolesCounterText = "0" + rolesCounter;
} else {
var rolesCounterText = rolesCounter;
}
$('#rolesColumn').append("<div id='roles-" + rolesCounter + "' class='div_row'><textarea name='ctl00$mainContent$uxRolesList$ctl" + rolesCounterText + "$uxTxtBoxRole' rows='5' cols='100' id='ctl00_mainContent_uxRolesList_ctl" + rolesCounterText + "_uxTxtBoxRole' MaxLength='2000' ></textarea><input type='submit' name='ctl00$mainContent$uxRolesList$ctl" + rolesCounterText + "$uxRemoveRoleBtn' value='X' id='ctl00_mainContent_uxRolesList_ctl" + rolesCounterText + "_uxRemoveRoleBtn' class='remove-roles-btn' style='vertical-align:top;' /><br /><span id='ctl00_mainContent_uxRolesList_ctl" + rolesCounterText + "_uxValTxtBoxRole' style='color:Red;visibility:hidden;'>Please complete this role requirement</span><br /><br /></div>");
e.preventDefault();
rolesCounter++;
$('#ctl00_mainContent_uxTxtBoxRolesCount').val(rolesCounter);
}));
So far so good. I hit the add button and the textbox appears, I type something in, everything's great. The html look something like this:
<div id="roles-0" class="div_row">
<textarea id="ctl00_mainContent_uxRolesList_ctl00_uxTxtBoxRole" cols="100" rows="5" name="ctl00$mainContent$uxRolesList$ctl00$uxTxtBoxRole">Cool Job1</textarea><input class="remove-roles-btn" type="button" value="X" style="vertical-align:top;"><br><br>
</div>
<div id="roles-1" class="div_row">
<textarea id="ctl00_mainContent_uxRolesList_ctl01_uxTxtBoxRole" maxlength="2000" cols="100" rows="5" name="ctl00$mainContent$uxRolesList$ctl01$uxTxtBoxRole">Test</textarea><input class="remove-roles-btn" type="submit" style="vertical-align:top;" value="X" name="ctl00$mainContent$uxRolesList$ctl01$uxRemoveRoleBtn"><br><br>
</div>
Then I hit submit and the new values do not come through.
In the C# side I'm trying to access the data using:
foreach (RepeaterItem item in dl.Items)
{
System.Web.UI.WebControls.TextBox rb = item.FindControl(control) as System.Web.UI.WebControls.TextBox;
if (rb.Text.Trim() != "")
{
PositionRequirement pr = new PositionRequirement();
pr.RequirementDescription = rb.Text;
pr.RequirementLevel = new PositionRequirementLevel(level, levelDescription);
pr.OrderNumber = i;
i++;
positionRequirements.Add(pr);
}
}
where dl = uxRolesList
control = uxTxtBoxRole
I'm at an utter loss as to why the new values are not coming through with the uxRolesList Repeater.
What am I doing wrong?
from what i know , the approach used is not going to show the items that are added within the Repeater Datasource, unless they exist before the page is being served to the user, so in the example, only the items that were bound to the repeater before leaving the server (if any) will show.
if you don't want to leave the page and make a trip to the server on every add click,of the top my head i would suggest that you would access them via the request Object using the name instead of the id ( Request[""] ) and keep the name of each textbox similar ("txtbox1","txtbox2")and append the count as you did in your Jquery code, then on the server when the page is submitted loop over the items using the counter that you have stored in uxTxtBoxRolesCount.