Not able to access Checkbox values in IE9 - asp.net

I have an asp.net web page where i have a checkbox control inside gridview control as under
<asp:GridView ID="grdMergeCoverage" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<input type="checkbox" id="chkSelectStream" onclick="OnSelectionChanged(this);"
testrunid='<%#DataBinder.Eval(Container.DataItem,"TestRunId") %>'
checked='<%#DataBinder.Eval(Container.DataItem,"CodeCoverageRequired") %>'
servername='<%#DataBinder.Eval(Container.DataItem,"ServerName") %>'
runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And the OnSelectionChanged function is as under
function OnSelectionChanged(id)
{
alert(id.testrunid);
alert(id.servername);
alert(id.checked);
}
In IE8 , I am able to get the value for testrunid,servername while in IE 9, it is coming as NULL.
What is the problem and how can i get the value in all the cases?

To get custom attributes you should use the getAttribute method.
Like so: id.getAttribute("servername").
The way you used to get the value before is non-standard and not always supported.

you can add the asp checkbox and add the attribute to it on the gridview rowitembound event. it will fire....

Related

PNM Sequence & asp.net: GridTemplateColumn should be mandatory

I use PNM Sequence. And I need to make one grid column as the mandatory field.
I know how to make it with any separate control. E.g. I can type:
<sq8:GridBoundColumn DataField="txtField" HeaderText="txtField"
SortExpression="txtField" UniqueName="txtField" FilterControlAltText="">
<ColumnValidationSettings>
<RequiredFieldValidator ForeColor=""></RequiredFieldValidator>
</ColumnValidationSettings>
</sq8:GridBoundColumn>
And I can use this Validator for the TextBox:
<sq8:Label runat="server" Text="Field:" ID="Label1" Width="100%"></sq8:Label>
<nobr>
<sq8:TextBox runat="server" ID="txtField" Width="100%"></sq8:TextBox>
<sq8:RequiredFieldValidator runat="server"
ErrorMessage="RequiredFieldValidator"
ID="RequiredFieldValidator4"
ControlToValidate="txtField"
SetFocusOnError="True">*</sq8:RequiredFieldValidator>
</nobr>
<sq:BindableControl runat="server" TargetControlID="txtField"
DataField="txtField"></sq:BindableControl>
And it works. User can't send the form because he gets an error - the field is empty.
But I need to do the same with grid.
When I open "Edit columns" in Grid Wizard I can't see any property as "mandatory" or something like this.
And the code with RequiredFieldValidator doesn't work with a grid column. If I try to use it:
<Columns>
<sq8:GridBoundColumn DataField="txtFieldGrid" HeaderText="txtFieldGrid"
SortExpression="txtFieldGrid" UniqueName="txtFieldGrid"
FilterControlAltText="">
<sq8:RequiredFieldValidator runat="server"
ErrorMessage="RequiredFieldValidator"
ID="RequiredFieldValidator4"
ControlToValidate="txtFieldGrid"
SetFocusOnError="True">*</sq8:RequiredFieldValidator>
<sq:BindableControl runat="server" TargetControlID="txtFieldGrid"
DataField="txtFieldGrid"></sq:BindableControl>
</sq8:GridBoundColumn>
</Columns>
In this case, I have an error:
Is there some method for grid column validation? Or it's impossible with a grid?
Maybe I can use some javascript?

how to get assign values to check box in grid view

i want to know status is active or not in check box. how to bind check box with database data. while editing im lossing previous data. data is not showing in check box
code behind updating event
cmd.Parameters.AddWithValue("#LeadsAccess", ChkLeads.Checked);
<asp:TemplateField HeaderText="Leads">
<ItemTemplate>
<asp:CheckBox ID="ChkLeads" runat="server" />
</ItemTemplate>
</asp:TemplateField>
db column- [Leads]
You can try like this
<asp:CheckBox ID="ChkLeads" runat="server"
Checked='<%#bool.Parse(Eval("columnName").ToString())%>' />
try this..
you can bind the db value like this..
<asp:CheckBox ID="ChkLeads" runat="server" AutoPostBack="true" Checked='<%#Convert.ToBoolean(Eval("LeadsAccess"))%>'/>

error message "String was not recognized as a valid Boolean" in aspx page

I have a SQL stored proc where I am creating a column ("Certified") dynamically based on two other columns. The value from this column is a '0' or '1'. The SQL stored proc query is:
, CASE WHEN
(StartMiles < EndMiles)
AND (StartTime < EndTime)
AND (bcd.Status != 'C')
THEN '1' ELSE '0' END
AS Certified
On the front end in my aspx page, I have a telerik radgrid that will display a checkbox (enabled if value is 1, disabled if value is 0). The aspx code is:
<telerik:GridTemplateColumn DataField="Certified" HeaderText="Certified" Visible="true">
<ItemTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="true"
OnCheckedChanged="CheckBox2_CheckedChanged"
Enabled='<%# !bool.Parse(Eval("Certified").ToString()) %>' />
</ItemTemplate>
</telerik:GridTemplateColumn>
I am getting an error on the aspx page String was not recognized as a valid Boolean
To resolve the error, how can I set a datatype in the stored proc?
<telerik:GridTemplateColumn DataField="Certified" HeaderText="Certified" Visible="true">
<ItemTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="true"
OnCheckedChanged="CheckBox2_CheckedChanged"
Enabled='<%# !Convert.ToBoolean(Convert.ToInt32(Eval("Certified").ToString())) %>' />
</ItemTemplate>
</telerik:GridTemplateColumn>
I would recommend using a code-behind method to do this instead of putting logic into the markup via embedded code blocks, like this:
Markup:
<telerik:GridTemplateColumn DataField="Certified" HeaderText="Certified"
Visible="true">
<ItemTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="true"
OnCheckedChanged="CheckBox2_CheckedChanged"
Enabled='<%# IsCertified(Eval("Certified").ToString()) %>' />
</ItemTemplate>
</telerik:GridTemplateColumn>
Code-behind:
protected bool IsCertified(string certifiedValue)
{
return !bool.Parse(certifiedValue);
}
Note: To be safer, I would recommend using the Boolean.TryParse() method instead of just the Parse(), as that will eliminate the chance of a string that cannot be parsed into a bool from throwing an exception. Read Boolean.TryParse Method documentation for more information.
This provides two advantages, in my opinion, over the OP code:
Simplified markup, because you do not have conditional logic in the markup, but now just a call to the method.
You can leverage the power of Visual Studio's IntelliSense, compiler to catch syntax errors at compile-time instead of run-time and the debugger itself.

ASP.NET Gridview: How do I stop a <span></span> within a Gridview from resetting when posting back?

I have a Gridview which contains some controls which are updated from the server when the user makes a selection from a gridview.
After making their selection from a drop down list the user can enter a percentage into a textbox on a row and I have some javascript which then carries out some calculations and outputs to a span within an item template in the Gridview.
This all works but if the user then makes another drop down list selection on any row all the spans in the gridview are reset to blank (I've tried with various controls, both asp.net and html).
This is all inside an Update Panel.
Code:
<asp:GridView runat="server" ID="gdvIngredients" AutoGenerateColumns="false" CssClass="table table-stripped"
GridLines="None" Visible="false" ShowFooter="true" EnableViewState="true">
<Columns>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label runat="server" ID="lblId" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Ingredient" ItemStyle-Width="12%">
<ItemTemplate>
<asp:DropDownList runat="server" ID="ddlName" AutoPostBack="true" OnSelectedIndexChanged="ddlName_SelectedIndexChanged"
ForeColor="Black" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="%" ItemStyle-Width="6%">
<ItemTemplate>
<asp:TextBox runat="server" Text="0" Width="50px" ID="txtPercentage" onkeyup="calculate(this);"
ForeColor="Black" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="kj" ItemStyle-Width="6%">
<ItemTemplate>
<asp:Label runat="server" ID="lblKj" CssClass="Kj" Style="display: none;" />
<span runat="server" id="spnKj" class="NewKj">0</span>
</ItemTemplate>
From your question what i have understood is that you are doing calculation in javascript and assigning it to span,but when next postback happens the value is not retained...If i am right i would suggest you to try the following steps.Use HiddenFields also to assign Values as HiddenFields Retain values during PostBacks.
Add Hidden field in Item Template
Set "calculate" function to Textbox from Codebehind(from rowdatabound event) & also pass the currosponding HiddenField Value to it as calculate(this,'HiddenFieldId');
protected void grd_RowDataBound(object sender,GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HiddenField hdn = ((HiddenField)e.Row.FindControl("HiddenFieldID"));
TextBox txtPercentage = (TextBox)e.Row.FindControl("txtPercentage");
HtmlGenericControl spnKj=(HtmlGenericControl )e.Row.FindControl("spnKj");
txtPercentage.Attributes["onClick"]="calculate(this,'"+hdn .ClientID+"');"
spnKj.innerHTML=hdn.Value;
}
}
Javascript:
Assign Calculated Value to Hidden Field...
function calculate(hdnID)
{
var CalcValue=//do your Calculation
document.getElementByID(hdnID).Value=CalcValue;
}
3.In the DropDown PostBack Event Bind the GridView Again.
The above code should take care of the Issue You are facing.
You can use the "AutoPostBack" property to control the Postback event of some controls.
Have a look if your dropdown list selection do any PostBack.
If it does, then span values disappear, becuase Span is not ASP.NET webcontrol. After pages is postback, asp.net webcontrol values are restored from ViewState. So, spans doesn't have ViewState and no values are restored.
You can try to change your tag to
<span runat="server" id="spanIDHere">SomeValue here</span>
runat="server" makes span to be HtmlGenericControl and maintains its ViewState.
Span values would not be saved after postback if you update them using Javascript. You should call server-side function to update span values or try to use Hidden fields to store your values.

Gridview Hidden Field - how can I get a hidden field value using JavaScript?

I have Gridview like this:
<asp:GridView ID="gvPartsSearchResult" runat ="server" CssClass="MRJ_TextGrid">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:RadioButton
ID="rdButton"
runat="server"
AutoPostBack ="true"
onclick="javascript:CheckOtherIsCheckedByGVIDMore()"/>
<asp:HiddenField
ID="hdnFileExtension"
runat="server"
Value ='<%#Bind("FILE_EXTENSION")%>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I want to read the hidden field value when the user clicks on the radio button. Can anyone help me with this?
One of the things about ASP.NET that is tough is that the ID's of your controls get mangled, This can make them tough to work with in JavaScript.
ASP.NET 4 offers a new ClientIDMode which eases this pain but until it is released we are stuck using workarounds. I wrote an article explaining the pros and cons of some of these workarounds - hopefully that will get you on the right track.
You could use some jQuery to make your JavaScript more unobtrusive and get rid of your inline event-handler on your radio button:
$(document).ready(function() {
$('table.MRJ_TextGrid input:radio').click(function() {
var hiddenValue = $(this).next().val();
// continue processing...
});
});

Resources