How to access Input type="Checkbox" control inside <dx:ASPxGridView/>(Dev Express) at server side(c#) - html

I have a dx:ASPxGridView (Dev express gridview) which contains HTML checkboxes - input type="checkbox" control.
Problem is how can i access these HTML checkboxes at server side, so that i can check those checkboxes according to condition.
Please suggest me how can i access these HTML checkboxes on server side.
Code image can be found here
Thanks
Girish Rawat

First, to access a HTML element on a server, you need to mark it with the runat="server" attribute.
To access your checkbox, handle the ASPxGridView.HtmlDataCellPrepared event. In the event handler, you can get your input by using the ASPxGridView.FindRowCellTemplateControl method. You will also need to set ID for the input.
<dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" OnDataBinding="ASPxGridView1_DataBinding" OnHtmlDataCellPrepared="ASPxGridView1_HtmlDataCellPrepared" KeyFieldName="ID">
<Columns>
<dx:GridViewDataCheckColumn VisibleIndex="0">
<DataItemTemplate>
<input id="check" type="checkbox" runat="server" />
</DataItemTemplate>
</dx:GridViewDataCheckColumn>
</Columns>
<Styles AdaptiveDetailButtonWidth="22"></Styles>
</dx:ASPxGridView>
protected void ASPxGridView1_HtmlDataCellPrepared(object sender, ASPxGridViewTableDataCellEventArgs e) {
HtmlInputCheckBox check = (HtmlInputCheckBox)ASPxGridView1.FindRowCellTemplateControl(e.VisibleIndex, e.DataColumn, "check");
check.Checked = GetValue((int)e.KeyValue);
}
public bool GetValue(int id) {
// your logic
return id % 2 == 0;
}

Related

How to use runat = "server" in input tag?

I'm writing in asp.net. When I add runat="server" property to my input tag, its value is null. When I remove runat="server" it works correctly. Who knows the reason?
I want change its property from code behind, that's why I wrote runat="server". However, the value is null.
protected void btnSaveChanges_Click(object sender, EventArgs e)
{
string current_id = Session["Current_user"].ToString();
string a = Request.Form["newusername"];
string b = Request.Form["newpassword"];
string c = Request.Form["rewpassword"];
}
Code for control:
<input type="text" name="newusername" placeholder="Enter Username" required="required" runat="server"/>
When you add runat="server" and make the simple HTML control to asp.net HTML control, then asp.net renders the id and the name of that control in a manner that does not conflict with other asp.net controls on the same page.
So change the input to: (note now I add id, and remove the name!)
<input type="text" id="newusername" placeholder="Enter Username" required="required" runat="server"/>
and get the value using the post like this:
Request.Form[newusername.UniqueID]
or using the value:
newusername.value
other links to consider:
Accessing control client name and not ID in ASP.NET
Use newusername.Value to access the value of the control in your server side function.
Like
protected void btnSaveChanges_Click(object sender, EventArgs e)
{
string current_id = Session["Current_user"].ToString();
string a = newusername.Value;
string b = newpassword.Value;
string c = rewpassword.Value;
}
You can't change an HTML control's properties using c# server side code. But you can do that using several other methods -
Method 1
you can use plain javascript or jquery to alter the HTML DOM element.
Method 2
you can create the HTML elements you want dynamically using HtmlGenericControl.
HtmlGenericControl username = new HtmlGenericControl("input");
username.attr("type", "text");
username.attr("name", "newusername");
And then you can append the controls into some div, like,
Front-End HTML code where you will add your dynamic controls-
<div id="dynaHtml" runat="server"></div>
Now, you can use that div to add your dynamic controls to page -
dynaHtml.Controls.Add(username);

adding variables to Server tag [duplicate]

This works:
<span value="<%= this.Text %>" />
This doesn't work:
<asp:Label Text="<%= this.Text %>" runat="server" />
Why is that?
How can I make the second case work properly, i.e., set the label's text to the value of the "Text" variable?
Use Data binding expressions
<asp:Label ID="Label1" runat="server" Text="<%# DateTime.Now %>" ></asp:Label>
Code behind,
protected void Page_Load(object sender, EventArgs e){
DataBind();
}
you can do this
<asp:Label ID="Label1" runat="server" ><%= variable%></asp:Label>
You will need to set the value of the server control in code
First of all, assign an ID to the label control so you can access the control
<asp:Label ID="myLabel" runat="server" />
Then, in your Page_Load function, set the value of your labels 'Text' field
protected void Page_Load(object sender, EventArgs e)
{
myLabel.Text = 'Whatever you want the label to display';
}
This function will be in your code behind file, or, if you are not using the code behind model, inside your aspx page you will need
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
myLabel.Text = 'Whatever you want the label to display';
}
</script>
Good luck.
In my code i am using something like this easily but in the databound control like ListView Item template
<asp:HyperLink ID="EditAction" class="actionLinks" Visible='<%#Eval("IsTrue").ToString() != "True"%>' runat="server" NavigateUrl='<%# Eval("ContentId","/articles/edit.aspx?articleid={0}")%>' />
But when i tried to use outside the databound control using <%# .. %>, it simply doesn't work.
You can easily do with
My href
But for server controls, and outside of databound control. We need to call DataBind() in pageload event explicitly
<asp:Hyperlink ID="aa" NavigateUrl='<%#myHref%>' >
Not sure how to mark this as such, but this is a bit of a duplicate. See this thread.
I don't think embedding code in to your markup will really make your markup any clearer or more elegant.
<asp:Label> is compiling at runtime and converting to html tags. You can set text with codebehind or like this:
<asp:Label id="Text1" runat="server" />
<% Text1.Text = this.Text;%>
UPD: Seems like my variant doesnt work, this is better:
protected void Page_Load(object sender,EventArgs e)
{
Text1.Text = this.Text;
}
Just pitching this little nugget in for those who want a good technical breakdown of the issue -- https://blogs.msdn.microsoft.com/dancre/2007/02/13/the-difference-between-and-in-asp-net/
I think the crux is in pretty decent agreement with the other answers:
The <%= expressions are evaluated at render time
The <%# expressions are evaluated at DataBind() time and are not evaluated at all if DataBind() is not called.
<%# expressions can be used as properties in server-side controls. <%= expressions cannot.

How to Pass Text Box Value to Query String Using HyperLink Field In Client Side Using Asp.Net

Iam developing application in asp.net c#, in that i have a requirement to pass text box value through query string without using server side code,
in that iam trying some code below, but its not working for me.
<asp:HyperLink ID="hlnkHistory" runat="server" Text="History" ForeColor="Green" ImageUrl="~/images/History2.png" Font-Bold="true" Target="_blank" NavigateUrl="~/WebFormReports/History.aspx?SerialNo=<%#Eval('txtSerialNo.ClientID')%>" ToolTip="View History"></asp:HyperLink>
When iam assign value directly like the below code its working fine.
<asp:HyperLink ID="hlnkHistory" runat="server" Text="History" ForeColor="Green" ImageUrl="~/images/History2.png" Font-Bold="true" Target="_blank" NavigateUrl="~/WebFormReports/History.aspx?SerialNo=1" ToolTip="View History"></asp:HyperLink>
Note: i need not server side code to pass value
like
protected void lnkNavigate_Click(object sender, EventsArgs e)
{
Response.Redirect("MyLocation.aspx?value=" + myTextBox.Text, false);
}
if it is not possible means please tell me is there any another option to pass text box value to query string like a href & link button
any help would be appreciated.. thanks in advance..
If you want to still use the asp:HyperLink then you can use this snippet.
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:HyperLink ID="hlnkHistory" runat="server" Text="History" ForeColor="Green" ImageUrl="~/images/History2.png" Font-Bold="true" Target="_blank" NavigateUrl="~/WebFormReports/History.aspx?SerialNo=" ToolTip="View History" onclick="setHyperlink()"></asp:HyperLink>
<script type="text/javascript">
function setHyperlink() {
var value_hyperlink = $("#<%=hlnkHistory.ClientID %>").attr("href");
var value_textbox = $("#<%=TextBox1.ClientID %>").val();
$("#<%=hlnkHistory.ClientID %>").attr("href", value_hyperlink + value_textbox);
}
</script>
If you don't need that hyperlink, you can use a somewhat simpler javascript function.
location.href = "~/WebFormReports/History.aspx?SerialNo=" + $("#<%=TextBox1.ClientID %>").val();

Issue related to inbuilt javascript function of Telerik controls

I have asp.net webapplication having some telerik controls.
i have a RadTextBox(txtSearch) and RadButton(btnSearch) on .aspx page.
i have written following validation for empty Textbox:
$('#btnSearch').click(function () {
if ($('#txtSearch_text').val() == '') {
$('#txtSearch_text').addClass('validation');
return false;
}
else {
$('#txtSearch_text').removeClass('validation');
}
});
in validation class i have set Border-left:2px solid red
now problem is that when i click on btnSearch it sets validation class to txtSearch textbox, but when i use mouseover on txtSearch textbox class name suddenly changed to someother from inbuilt javascript function of Telerik. in this Javascript function of telerik TextBox, it changes class name of textbox to another class.
and this execution of change class occurs after executing custom javascript function.
so i want to execute customer javascript function after executing inbuilt functions of telerik. how to do it?
Thanks
You can define invalid states for RadInputs. RadTextbox by itself cannot be invalid because you can put anything in a textbox (unlike a numeric textbox, for example), yet here is a starting point:
<telerik:RadTextBox ID="RadTextBox1" runat="server">
<InvalidStyle BackColor="Red" />
</telerik:RadTextBox>
<asp:RequiredFieldValidator ID="TextBoxRequiredFieldValidator" runat="server" Display="Dynamic"
ControlToValidate="RadTextBox1" ErrorMessage="The textbox can not be empty!">
</asp:RequiredFieldValidator>
<telerik:RadButton ID="RadButton1" runat="server" OnClientClicked="test" Text="submit"
AutoPostBack="false" />
<script type="text/javascript">
function test() {
var tb = $find('RadTextBox1');
if (tb.get_value() == "") {
$find('RadTextBox1')._invalidate();
$find('RadTextBox1').updateCssClass();
}
}
</script>
Tampering directly with the HTML of complex controls may get you nowhere because they will try to update/fix their state according to the logic they have.

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.
}