How to get value from dynamic control in gridview using javascript - javascript

I have one dynamic gridview contains one textbox.when user lost focus from the textbox, i want to get the value using javascript.for that i am using onblur() event in the textbox.but i'm not able to get the value .please find the code below
function validateintforDWOnblur(input) {
var row = input.parentNode.parentNode;
var rowindex = row.rowIndex;
var totaldw = document.getElementById('<%=gdvaddmsrcrt.ClientID%>').rows[rowindex].cells[2].innerHTML;
var totaldw1 = document.getElementById(totaldw.id).value;
var dw = document.getElementById('<%=gdvaddmsrcrt.ClientID%>').rows[rowindex].cells[3].innerHTML;
return true;
}
textbox inside the gridview
<asp:TextBox ID="txtdiswei" runat="server" onblur="javascript:return validateintforDWOnblur(this)" Width="100px" Text='<%# Bind("DW") %>' Height="40" CssClass="form-control"></asp:TextBox>
Please help me to sort this out.

I'm not sure that it is what you want but why don't you try
var value = document.getElementById('<%=yourTextBox.ClientID%>').value;
so for your table you can pass the ClietnId property from eachRow and use it in the javascript function
function doSomething(idOfTheTextBox){
var value = document.getElementById(idOfTheTextBox).value;
}
in html
onblur="doSomething('<%# this.ClientID %>')
if you build server side your rows you can do the following
TextBox txt = new TextBox();
txt.ID = "something1";
txt.Text = "text";
txt.Attributes.Add("onBlur","doSomething('" & txt.ClientID & "')");
CellOfTheGrid.Controls.Add(txt);

Related

Getting the value out of a dynamic asp:DropDownList

Basically, I have a drop down list with a ID and runat="server" tag and is inside a asp:TableCell. The original DDL has no options, but through javascript, I am populating the dropdown with some options that update my table on the front end. On button click, through my vb.net code, I need to retrieve the text inside the ddl. Any suggestions of how to do this would be nice. Thank you in advance, and if you need any more information from me please let me know.
P.S. I am not able to use ajax with this project.
[HTML] - Just showing the tablecell and ddl
<asp:TableCell ID="ocProduct">
<asp:DropDownList ID="myDropDown" CssClass="ocProduct"
OnChange="indexChanged(this);" runat="server">
</asp:DropDownList>
</asp:TableCell>
[VB.net]
Protected Sub updateWeight_Click(Sender As Object, e As EventArgs)
msgbox(myDropDown.text) ' does not work
msgbox(mytable.rows(1).cells(0).text) ' does not work
msgbox(myTable.rows(1).cells(0).controls(0).toString) ' does not work
msgbox(myDrowDown.selectedValue) ' does not work
' All of these are returning ""
end sub
[JavaScript] - This is just showing how I load the ddl
var ddl = document.getElementById('myDropDown');
var tempOption = document.createElement('option');
tempOption.text = "Please select an option..."
tempOption.value = 0;
ddl.options.add(tempOption);
for (var i = 1; i <= counter; i++) {
var option = document.createElement('option');
var tempArray = parsePerHash(i);
option.text = tempArray[0];
option.value = i;
ddl.options.add(option);
}
};
You have to use the specific properties of that control.
Try using "msgbox(myDropDown.SelectedValue)". You can see the list of properties here: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.dropdownlist_properties(v=vs.110).aspx

How to change text for a Button with jQuery in ASP.NET

I tried this code on jQuery to change the btn text when I click on a span that opens a modal to add a ShipWay.
$(document).ready(function () {
$("#spanOpen").click(function () {
$("#fulldiv").fadeIn(1000);
$('#titleModal').text('הוספת שלוחה');
$("#ContentPlaceHolder1_btnAddShip").prop('value', 'הוסף');
$('#ContentPlaceHolder1_txtShipName').val('');
$('#ContentPlaceHolder1_txtShipPrice').val('');
$('#ContentPlaceHolder1_txtShipStart').val('');
$('#ContentPlaceHolder1_txtShipEnd').val('');
$('#ContentPlaceHolder1_txtShipPremium').val('No');
});
$(".close").click(function () {
$("#fulldiv").fadeOut(500);
});
});
function UpdateClick(btn)
{
$("#fulldiv").fadeIn(1000);
var row = btn.parentNode.parentNode;
var rowIndex = row.rowIndex - 1;
$('#ContentPlaceHolder1_txtShipName').val(row.cells[6].innerHTML);
$('#ContentPlaceHolder1_txtShipPrice').val(row.cells[5].innerHTML);
$('#ContentPlaceHolder1_txtShipStart').val(row.cells[4].innerHTML);
$('#ContentPlaceHolder1_txtShipEnd').val(row.cells[3].innerHTML);
$('#ContentPlaceHolder1_txtShipPremium').val(row.cells[2].innerHTML);
$('#titleModal').text('עדכן שלוחה');
$("#ContentPlaceHolder1_btnAddShip").prop('value', 'עדכן');
return false;
}
This is the button I use in my html for Add/Update the row:
<asp:Button ID="btnAddShip" runat="server" ValidationGroup="AddShip" Text=""
CssClass="btn" OnClick="btnAddShip_Click" />
When I tried to check his text in the code behind he said that he have nothing like = "".
This is my code in the C#
In the first if I want to ask if the text of the button is Add
and if not it will be Update in the else
the code go for WebService and to Xml
I tried the code he works well I just need that if will work perfect
because I don't want to create another button
ElectricWSL.Ship ship = new ElectricWSL.Ship();
ship.ShipName = txtShipName.Text.Trim();
ship.ShipPremium = txtShipPremium.Text.Trim();
ship.ShipPrice = double.Parse(txtShipPrice.Text);
ship.ShipStartTime = txtShipStart.Text.Trim();
ship.ShipEndTime = txtShipEnd.Text.Trim();
if (btnAddShip.Text == "הוסף")
{
ws.WSLAddShip(ship);
ShowXMLGrid();
}
else
{
int rowIndex = GetRow(txtShipName.Text);
ws.WSLUpdateShip(ship, rowIndex);
ShowXMLGrid();
}
Answering one of your point in details when i tried to check his text in the code behind it contains like =""
Button text is empty in code because
<asp:Button ID="btnAddShip" runat="server" ValidationGroup="AddShip" Text=""
CssClass="btn" OnClick="btnAddShip_Click" />
Add some text to it like
<asp:Button ID="btnAddShip" runat="server" ValidationGroup="AddShip" Text="Add"
CssClass="btn" OnClick="btnAddShip_Click" />
And get it in code behind like
if (btnAddShip.Text == "Add")
{
ws.WSLAddShip(ship);
ShowXMLGrid();
}
else
{
int rowIndex = GetRow(txtShipName.Text);
ws.WSLUpdateShip(ship, rowIndex);
ShowXMLGrid();
}

How to pass value from javascript to label in asp.net?

I need to pass a javascript value to label in asp.net. The javascript function was inside JScript1.js. I pass the value to a hidden field. I already added the script to the source of the content page as below, but doesn't work whenever I called the function value to the vb.net code behind.
<script src="JScript1.js" type="text/javascript"></script>
Here's the javascript function inside a JScript1.js
function dateTimeToday()
{
var month=new Array();
month[0]="1";
month[1]="2";
month[2]="3";
month[3]="4";
month[4]="5";
month[5]="6";
month[6]="7";
month[7]="8";
month[8]="9";
month[9]="10";
month[10]="11";
month[11]="12";
var d = new Date();
var mt=month[d.getMonth()];
var h=d.getHours();
var m=d.getMinutes();
var s=d.getSeconds();
mt=checkMonth(mt);
m=checkTime(m);
s=checkTime(s);
var x = document.getElementById("HiddenField1");
x.innerHTML=d.getFullYear()+"-"+mt+"-"+d.getDate()+" " +h+":"+m+":"+s;;
t=setTimeout(function(){myFunction()},500);
}
function checkTime(i)
{
if (i<10)
{
i="0" + i;
}
return i;
}
function checkMonth(j)
{
if (j<10)
{
j="0" + j;
}
return j;
}
Below is my code of calling the javascript value.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
lbl.Text = HiddenField1.Value
End If
End Sub
Am I missing something on my code?
Please help.
Thanks in advance.
You need to set the value of the input field, not the innerHtml:
var x = document.getElementById("HiddenField1");
x.value =d.getFullYear()+"-"+mt+"-"+d.getDate()+" " +h+":"+m+":"+s;
And consider that the ID of asp.net control can change when the page is rendered, you could use:
var x = document.getElementById('<%= HiddenField1.ClientId %>');
x.value =d.getFullYear()+"-"+mt+"-"+d.getDate()+" " +h+":"+m+":"+s;
I hope you want to set value in Hiddenfield in javascript..You can do..
document.getElementById("HiddenField1").value=d.getFullYear()+"-"+mt+"-"+d.getDate()+" " +h+":"+m+":"+s;
In code behind..
lbl.Text = HiddenField1.Value;
Hidden input markup..
<input id="HiddenField1" type="hidden" runat="server" clientidmode="Static" value=""/>

How can I assign one value to column by js in RadGrid

I have Radgrid with all rows always in edit mode. I want following functionality in one of the columns: after item is edited, all rows in this colum take this value. Here is how my column looks like.
<telerik:GridTemplateColumn HeaderText="Opis" HeaderStyle-Width="125px" ItemStyle-Width="120px"
UniqueName="poz_nazwa">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem, "poz_nazwa")%>
</ItemTemplate>
<EditItemTemplate>
<telerik:RadTextBox runat="server" ID="Rtopis" DataTextField="poz_nazwa" DataValueField="poz_nazwa"
Width="120px" Text='<%#Bind("poz_nazwa") %>' onfocus="javascript:podp(this.id);"
AutoCompleteType="Disabled" onpropertychange="Opisblur()">
</telerik:RadTextBox>
</EditItemTemplate>
</telerik:GridTemplateColumn>
And here is what i tried and is not working:
function OpisBlur() {
if (event.propertyName == 'value') {
var grid = $find("<%=RadGPozycje.ClientID %>");
var masterTableView = grid.get_masterTableView();
var iloop;
if (masterTableView != null) {
var gridItems = masterTableView.get_dataItems();
var i;
for (i = 0; i < gridItems.length; ++i) {
var gridItem = gridItems[1];
var cell = gridItem.get_cell("poz_nazwa");
var controlsArray = cell.getElementsByTagName('input');
if (controlsArray.length > 0) {
var rdo = controlsArray[0];
rdo.value = "valueofchangeditem";
}
}
}
}
}
There are two most obvious problems with my approach:
I change only selected item, not all. When i try to use masterTableView.get_editItems() IE says that there is no such method.
This code produce stack overflow since function occurs on propertychange, and inside of it i change property.
Can you help me find solution to implement desired functionality?
In your ItemTemplate for the column, can you put a label or something that has a className? Then wouldn't you be able to update everything on the page that has that class with the new value?
$('.poz_nazwaClass').val("valueofchangeditem");
If you don't use jQuery, adding a tag then, element.getElementsByTagName('poz_nazwaTag') then loop through the elements I think.

How to get custom attribute value from a radcomboboxitem when an item is selected with javascript

Here is my code:
<telerik:RadComboBox ID="ddlServicesRequested" runat="server" DataValueField="Value" DataTextField="Text" Skin="Vista" OnClientSelectedIndexChanging="setQtyReq"></telerik:RadComboBox>
function setQtyReq(combo, eventArgs) {
var Combo = $find("<%= ddlServicesRequested.ClientID %>");
var index= eventArgs.get_item().get_index();
}
Me.ddlServicesRequested.Items.Insert(0, New RadComboBoxItem("-- Select One --", "0"))
Dim dt1 As DataTable = GetDataTable("myStoredProcedure")
For Each dr As DataRow In dt1.Rows
Dim rcbi As New RadComboBoxItem()
rcbi.Text = dr("Text")
rcbi.Value = dr("Value")
rcbi.Attributes("Min") = dr("MinQty")
rcbi.Attributes("Max") = dr("MaxQty")
Me.ddlServicesRequested.Items.Add(rcbi)
Next
Here I have the radcombobox that is being populated with two custom attributes, Min and Max. When the user selects an item the setQtyReq javascript function get called and I am able to get the radcombobox and the index of the selected item. My problem I am having is that I can't figure out a way to get the values of the custom attributes. How can I get the values of the custom attributes?
Finally figured out how to do it:
function setQtyReq(combo, eventArgs) {
var item = eventArgs.get_item();
var min = item.get_attributes().getAttribute("Min");
var max = item.get_attributes().getAttribute("Max");
}
http://www.telerik.com/help/aspnet-ajax/combobox-custom-attributes.html
<script language="javascript">
function setQtyReq(combo, eventArgs) {
var Combo = $find("<%= ddlServicesRequested.ClientID %>");
var index= eventArgs.get_item().get_index();
alert(Combo.Items[index].Attributes.Max);
alert(Combo.Items[index].Attributes.Min);
}
</script>
Looks like the above will work.

Categories

Resources