Set a ViewState using JQuery - c#

I have a CustomWebUserControl of a GridView and to move around the items I use a ViewState, now I need to move the SelectedIndex when I click on the row, the process gonna be:
Click in the expected row;
Set the value of rowIndex in a currentItemTextBox.Text;
This value gonna be input on the ViewState using the TextChanged event;
I'll Call a DataBind for my GridView to my internal methods select the currentRow using this ViewState.
<script type="text/javascript">
$(document).ready(function () {
$("#<%=mainGridView.ClientID%> tr").click(function () {
var ind = $(this).index();
$("#<%=currentIndexTexBox.ClientID%>").val(ind);
});
});
</script>
*When the script set the value the TextChanged event isn't call.
I hope that you can understand my scenario.enter code here
Thanks!

Try to add AutoPostBack="True" in you TextBox. This will call the OnTextChanged event.
<asp:TextBox ID="currentIndexTexBox" runat="server" OnTextChanged="CurrentIndexTextBoxOnTextChanged()" AutoPostBack="True"></asp:TextBox>

Related

CheckList Box inside a gridview shows alert only when if it is not postback as the function is called on rowbound of grid

I have CheckListBox inside a GridView. I need an alert when the checkbox gets clicked. It is currently done on the Gridview.RowDataBound() event, so it happens only one time in if it is not postback. How do I make the alert to work every time when the page loads.
You can do it using jquery like below:
<head>
<title></title>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$(document).on("click", "input:checkbox", function () {
alert(this.checked);
});
});
</script>
</head>
Make sure you're doing something like this in your RowDataBound event.
protected void gvTest_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var cbListSource = getDataSource(); // get the data source values for the checkboxlist to bind to
CheckBoxList cblItems = (CheckBoxList)e.Row.FindControl("cblItems");
cblItems.DataSource = cbListSource;
cblItems.DataTextField = "QuestionName";
cblItems.DataValueField = "QuestionId";
cblItems.DataBind();
cblItems.Attributes.Add("onclick", "alert('Your alert text goes in here')");
}
}
Alternatively you can use javascript or jquery to achieve the same result.

How to select just a radiobutton in a repeater with postback

As I found on Internet that there is a bug from MS when we add a RadioButton inside repeater for selecting just one.
But I found this code here that solved my problem. (only one radiobutton selection in repeater)
$(function () {
$('[name$="$YourGroupName"]').attr("name", $('[name$="$YourGroupName"]').attr("name"));
$('[name$="$YourGroupName"]').click(function () {
//set name for all to name of clicked
$('[name$="$YourGroupName"]').attr("name", $(this).attr("name"));
});
});
But it only works when the radiobutton is not set to autopostback. But I need to do a postback when I select a radiobutton and a response to the database. But.... anytime that I do a click on a radiobutton with postback, always the first in the list is selected and the ItemDataBound it's not working because of the jquery function which it's renaming all the radiobuttons with the same name.
Suggestions?
Try to add this script instead of the one you're using at the end of the page after the repeater :
<script>
var radios = $("input:radio");
radios.click(function () {
radios.removeAttr('checked');
$(this).prop("checked", true);
//$(this).attr('checked', 'checked'); // jQuery < 1.6
return true;
});
</script>
This will allow the AutoPostBack of your radiobutton to work again.

asp.net - problems getting dropdownlist

I am using a code behind function to bind my dropdownlist dynamically, when a user changes the dropdownlist and submit a purchase, the selectedvalue is always empty.
I have tried both ddl.SelectedItem.ToString(); and ddl.SelectedValue.ToString(); but none work. Also for these 2 code behind functions below, I can't seem to use void methods instead of a function that needs a returning value and a parameter, is there anyway to use void methods without parameters? Any advice is appreciated.
Thanks.
<%# FormattedSize((string)Eval("Size")) %>
<%# FormattedGetSize((string)Eval("Size")) %>
inline:
<asp:DropDownList ID="DropDownList1" runat="server" OnDataBinding='<%# FormattedSize((string)Eval("Size")) %>'></asp:DropDownList>
<a href='AddToCart.aspx?CategoryId=<%# Eval("CategoryId") %>&&ProductId=<%# Eval("ProductId" ) %>&&Size=<%# FormattedGetSize((string)Eval("Size")) %>' style="border: 0 none white;">
Code Behind:
protected string FormattedSize(string size)
{
if (size.Contains("s"))
{
DropDownList ddl = (DropDownList)FormView_Product.Row.Cells[0].FindControl("DropDownList1");
ddl.Items.Add("S");
}
if (size.Contains("m"))
{
DropDownList ddl = (DropDownList)FormView_Product.Row.Cells[0].FindControl("DropDownList1");
ddl.Items.Add("M");
}
if (size.Contains("f"))
{
DropDownList ddl = (DropDownList)FormView_Product.Row.Cells[0].FindControl("DropDownList1");
ddl.Items.Add("Freesize");
}
return null;
}
protected string FormattedGetSize(String Size)
{
DropDownList ddl = (DropDownList)FormView_Product.Row.Cells[0].FindControl("DropDownList1");
string selectedSize = ddl.SelectedItem.ToString();
return selectedSize;
}
The reason why it doesn't work is because.. "you're doing it wrong". You're expecting that the <a href=.. will change based on the user interaction but instead it is already generated when user receives the page. If you want the link to change based on the dropdown, you'd have to have either:
a postback on dropdown selection, then the link would change...
you could change the href with some javascript by attaching event on dropdown selection
What you do in your Page_Load method? Do you check to see if the current request is a post back or not (using IsPostBack)?
If so, check for IsPostBack and bind your DropDownList to the underlying data source only on Get requests.

problem with check box in asp .net c#?

The controls on aspx page are like this
Submitted
Submission Date
I want, if the check box is checked, then the textbox will be enabled I wrote
if(chkSubmitted.Checked)
{
txtSubmissionDate.Enabled = true;
}
in the page load event. But when the page is loaded this checkbox having no effect on. Whats going wrong?
If you want the action of clicking the checkbox to enable the textbox, you'll need to do a postback when the box is checked by setting AutoPostBack="True":
<asp:CheckBox runat="server" ID="chkSubmitted" AutoPostBack="True" />
Or, you could use JavaScript:
<asp:CheckBox runat="server" ID="chkSubmitted" onclick="setSubmissionDateEnabled()" />
function setSubmissionDateEnabled()
{
var chkSubmitted = document.getElementById("<%= chkSubmitted.ClientID %>");
var txtSubmissionDate = document.getElementById("<%= txtSubmissionDate.ClientID %>");
txtSubmissionDate.disabled = !chkSubmitted.checked;
}
First set autopostback property to true of checkbox
write following code in checkbox_Selectedindexchanged event
if(chkSubmitted.Checked)
{
txtSubmissionDate.Enabled = true;
}
else
{
txtSubmissionDate.Enabled = false;
}
Thats most likely because the default state of txtSubmissionDate is enabled already.
Try this in your page_load:
txtSubmissionDate.enabled = !chkSubmitted.Checked
To clarify, the textbox should not (!) be enabled when the Checkbox is.
Put this in Page_PreRender event. At this stage it will capture the user affected state of chkSubmitted.

How can i get ASP.Net Grid Element using Javascript

I am using a Grid View in asp.net, i want to get Element on clicking a grid, how can i do so?
A grid has a column id, name, warp, weft, etc, i want to pick the selected cell data using Javascript, let me know.
Please help...
Regards
Atif
To track which row button is clicked, you have to set the row Index as a parameter to a JS function like...
protected void grdForecast_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType ==DataControlRowType.DataRow )
{
((Button)e.Row.FindControl("buttonId")).Attributes.Add("onclick", "javascript:update(" + (e.Row.RowIndex ) + ");");
}
}
And then in JavaScript:
<script language="javascript" type="text/javascript">
function update(ri) {
var grd = document.getElementById('<%= GridView1.ClientID %>');
SecondCellValue = grd.rows[ri].cells[1].childNodes[0].value
ThirdCellValue = grd.rows[ri].cells[2].childNodes[0].value
}
</script>
Do you have a control inside the cell that you can reference? If not, then you can create a hidden control. Then you can write out the control's client id to the client side in the PreRender event handler via ScriptManager. And you can then get a hold of that element by id and find other content inside that parent cell.
Alternatively, you can use jquery to handle cell click events...
$('#myTable td').click(function () {
alert($(this).html());
});

Categories