How to click elements in C# - c#

I got a form with a radio button (button with value 3) that I need to select and a submit button that need to be clicked. The problem is I don't know how to select the radio buttons or click the button since it doesn't have a name or id.
Here is the HTML code:
<input type="radio" name="chosen" value="3" id="a3">
<input type="radio" name="chosen" value="2" id="a2">
<input type="radio" name="chosen" value="1" id="a1">
<input value="Next" type="submit">
Here is the code I've tried for the button (I'm trying to select the button with value 3:
webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("chosen")[2].InvokeMember("click");
but that code won't work and I don't know how to click the button to submit and go on. I need help selecting the radio button and click the submit button. I don't have access to change any of the HTML code.

I'm not quite sure what you meant by "use C# to click". Because basically, the element is displayed and interact in browsers (Chrome, FireFox). And C# is for processing http requests in server (IIS).
So to select/click some html element, you can use jQuery such as
$('input[value="3"]').click();

Related

Radio Submit Button Form and Redirect

I am wanting to redirect to another page but at the same time being able to grab the details of the button that was selected. I was reading up on how onsubmit works with HTML and radio buttons work. Prior to adding buttons, I had a button and whenever it was clicked it would redirect me to the next page. I still want to do the same thing, just being able to add radio buttons to the view and submit that radio button so that way I can grab the information from the button that was selected.
I attempted:
#{
ViewData["Title"] = "Index";
}
<h2>Customer</h2>
<form method="POST">
<input type="radio" value="1" /><label>Valid</label>
<input type="radio" value="2" /><label>Wrong</label>
<input type="radio" value="3" /><label>InValid</label>
<a href="#("window.location.href='" + #Url.Action("SecIndex", "Second") + "'");">
<input type="submit" value="Address Validation" />
</a>
However, this does not redirect me to the page that I needed it to redirect to. I also noticed that once I select buttons I cannot unselect, is that apart of the radio button feature?
I also noticed that once I select buttons I cannot unselect, is that apart of the radio button feature
Yes. That's how it works.
I still want to do the same thing, just being able to add radio buttons to the view and submit that radio button so that way I can
grab the information from the button that was selected.
If you want to post the selected value to backend, you could set name for radio buttons. Because model binding system will bind value by name.
View:
<form method="POST" asp-action="SecIndex" asp-controller="Second">
<input type="radio" value="1" name="Status"/><label>Valid</label>
<input type="radio" value="2" name="Status"/><label>Wrong</label>
<input type="radio" value="3" name="Status"/><label>InValid</label>
<input type="submit" value="Address Validation" />
</form>
Controller:
public class SecondController : Controller
{
[HttpPost]
public IActionResult SecIndex(string Status)
// you can get "1" or "2" or "3" which based on your checked radio button
{
return RedirectToAction("Privacy");
}
}
HTML doesn't have store capability. you can't grab data without a programming language. But you can click to redirect to another page.
Use the button tag and use the anchor tag in the button, rather than -

can't click on a button (element is not visible) c# webdriver

I am trying to click on a button, but I get "element is not currently visible and so may not be interacted with", how can I fix this?
I tried using commands like:
driver.FindElement(By.Id("btnSave")).Click();
driver.FindElement(By.Xpath(".//*[#id='btnSave']")).Click();
sorry I did not understand how to use html properly in comments(just deleted all the <>)
button id="btnSave" class="btn btn-primary" type="submit" value="save" data-target="#" name="command"
i aria-hidden="true" data-icon="Z" /i
span class="title" Saugoti /span
/button
I just found out that there is a hidden element with the same id, so how do I deal with this situation?
This means that you've located the "Save" button that is invisible.
You should improve your location strategy. For instance, look for the button inside a specific form:
driver.FindElement(By.CssSelector("form#myform button#btnSave")).Click();
Or, just get all the buttons and choose the one by index:
driver.FindElements(By.Id("btnSave"))[1].Click();

how to determine which textbox is focused and add text to it?

I am creating web application in asp.net. I have 5 text box which are asp controls. I want to get which text box having focus on that time and then when I am press "add" button, the "hello" text has to added to the particular text box which are got focused before pressed the "add" button.
I need Asp.net (with C# code) to move to my next step
Since the last focus will be on the Add button, you need to "remember" the TextBox before the Button is clicked.
Try to use onfocus() event.
<!-- Add a HiddenField control to "remember" the id of a TextBox -->
<asp:HiddenField ID="HiddenField1" runat="server" />
<!-- Assign current focus to that hidden field -->
<asp:TextBox onfocus="document.getElementById('HiddenField1').value=this.id" ...>
<asp:TextBox onfocus="document.getElementById('HiddenField1').value=this.id" ...>
If that works, you will be able to get a value in code-behind as
string c = HiddenField1.Value;
if (!string.IsNullOrEmpty(c))
if (c == "TextBox1")
TextBox1.Text = "hi there";
Note: depends on your environment you might need to call ClientID (e.g. getElementById('<%=HiddenField1.ClientID%>')
UPDATE
As I told you above you might need to use HiddenField1.ClientID to find the HiddenField Control. When your source looks as
<input type="hidden" name="ctl00$MainContent$HiddenField1" id="MainContent_HiddenField1" />
<input name="ctl00$MainContent$TextBox1" type="text" id="MainContent_TextBox1"
onfocus="document.getElementById('HiddenField1').value=this.id" />
<input name="ctl00$MainContent$TextBox2" type="text" id="MainContent_TextBox2"
onfocus="document.getElementById('HiddenField1').value=this.id" />
<input type="submit" name="ctl00$MainContent$Button1" value="Button" id="MainContent_Button1" />
That means that you use MasterPage and asp.net changes id of your control to MainContent_HiddenField1. So when you use document.getElementById('HiddenField1') it will not find such control because its id is different. You either need to use
document.getElementById('MainContent_Button1').value=this.id
which will work but might require changes if you remove master page or will move the textbox to other container
or use
document.getElementById('<%=HiddenField1.ClientID%>').value=this.id
which will automatically inject correct id (recommended asp.net way)

Can I add HTML button triggers to update panel

Is it possible to add HTML input buttons to asp.net triggers, as I have a message box it works perfectly for a gridview which is in update panel,
but when I go to a different page of gridview, message box displays but buttons stops working, I don't know how to debug it, please help.
this is the button,
<input type="button" id="Button2" value="Cancel" cssclass="rightButton" />
and can I add it to,
<asp:AsyncPostBackTrigger ControlID="Button2" EventName="Click" />
OR should I not ?
with runat server tag you use html controls in c# script.
i.e
<input type="button" id="Button2" value="Cancel" cssclass="rightButton" />
should be
<input type="button" id="Button2" value="Cancel" cssclass="rightButton" runat="server" />
and the when you double click on it(assuming VS as IDE), it will make a new click event in c# snippet. Besides its good practice to use direct html tags when complex functions are not needed, it saves time to display the page, as an asp component first translates itself in the html and then goes to browser; while this approach saves time of translation.
Regards
Yes you can! to see errors ,first remove UpdatePanel from your code and then test elements without it,in this status if any error occurs will be shown, after test you can add it again.

How to get or set Selected index of the Radio Button List using jquery?

How I can radio button checked using index..
Below is my asp.net C# code for radion button list...
<asp:RadioButtonList runat="server" ID="rdlCategory" CssClass="clsradio" AppendDataBoundItems="true" RepeatDirection="Horizontal" RepeatLayout="Flow" >
</asp:RadioButtonList>
and Bind it using C# dynamically..
and Html looks like
<span class="clsradio" id="ctl00_cphTop_rdlCategory"><input type="radio" value="8" name="ctl00$cphTop$rdlCategory" id="ctl00_cphTop_rdlCategory_0">
<label for="ctl00_cphTop_rdlCategory_0">category1</label>
<input type="radio" value="11" name="ctl00$cphTop$rdlCategory" id="ctl00_cphTop_rdlCategory_1">
<label for="ctl00_cphTop_rdlCategory_1">category2</label>
<input type="radio" value="22" name="ctl00$cphTop$rdlCategory" id="ctl00_cphTop_rdlCategory_2">
<label for="ctl00_cphTop_rdlCategory_2">category3</label>
<input type="radio" value="33" name="ctl00$cphTop$rdlCategory" id="ctl00_cphTop_rdlCategory_3">
<label for="ctl00_cphTop_rdlCategory_3">category4</label>
<input type="radio" value="34" name="ctl00$cphTop$rdlCategory" id="ctl00_cphTop_rdlCategory_4">
<label for="ctl00_cphTop_rdlCategory_4">category5</label>
</span>
I want to add attribute checked=true to radio button by index value .
suppose, I passed index=2 then it should be selected Category2 ..
or also want to get selected (checked=true) radio button index using in jquery..
How I do this?
Unlike select boxes, there isn't really a concept of an index in radio buttons.
However you can do something like this in jQuery:
$(".clsradio input:checked").index();
Which will get all the input boxes within your span that are checked (which as your using radio's should only be one, if only one group is inside .clsradio).
Here is an example on jsfiddle
EDIT:
A more full proof example (but slower) would be:
$("input[name$='rdlCategory']:checked").index();
Which gets inputs ending in rdlCategory. You could even add ":radio" but you don't need to.
EDIT 2 - Checking by passing in index.
You can use:
$(".clsradio input:nth-child(5)").attr("checked", "checked");
nth-child using the "input" selector can be used as the index.
use index method of jquery
function selectRadioButton(inputindex){
$('input[name="ctl00$cphTop$rdlCategory"]').index(inputindex).attr('checked', true);
}
to get selected radio button index...
var selectedIndex = $('input[name="ctl00$cphTop$rdlCategory"]').attr('checked', true).index(inputindex);
$("#<%= rdlCategory.ClientID %> input:checked").index();
As for how select items in ASP.NET using jQuery, i'm not sure it will work in all scenarios. ASP.NET updates ViewState each time you touch ASP.NET control thus i wouldn't use much jQuery to manipulate values of selection
if you still want this then $(selector).attr("checked",true);
This is working for me:
$($("input[name='GroupName']").get(index)).prop('checked', true);
Explanation:
$("input[name='GroupName']") returns a list of items that have the name 'GroupName'
.get(index) returns the ith item in the list. In the case of a radio group this would be the input tag you want.
The input tag is an HTML element, not a jQuery item, so you re-wrap it. Then you can call the prop method.
$(html).prop('checked',true)
Unchecked will give "undefined" otherwise it will return the value of the selected object.
$('input[name=ctl00$cphTop$rdlCategory]:checked').val()

Categories