I have a grid view, which contains a dropdownlist and a panel that i want to make invisible and visible by the selected value of the dropdownlist.
The javascript code which works when not used with the gridview is:
function showPannel(panelId,dropdownId ) {
var panel = document.getElementById(panelId);
var dropDown = document.getElementById(dropdownId);
if (dropDown.options[dropDown.selectedIndex].value = 'Diesel Deals') {
panel.className = "visibleDiv";
}
else{
panel.className = "hiddenDiv";
}
}
i'm passing the panelId and dropdownlist id from here:
if (e.Row.RowType == DataControlRowType.DataRow)
{
Panel p = (Panel)e.Row.FindControl("Panel1");
DropDownList t1 = (DropDownList)e.Row.FindControl("DropDownList1");
t1.Attributes.Add("onchange",
string.Format("javascript:showPannel('{0}', '{1}')",p.ClientID, t1.ClientID ));
}
but it is not working. The function is getting called, but its giving undefined when dropDown.options[dropDown.selectedIndex].value is alerted.
I tried to do
Gridview1 = document.getElementById('<%=GridView1.ClientID%>');
var cell = Gridview1.rows[0].cells[2];
var dropdownlist = cell.childNodes[0];
var dropdownSelectedValue = dropdownlist.options[dropdownlist.selectedIndex].value;
alert(dropdownSelectedValue);
but its not working either.
Please help
Thanks
Panel p = (Panel)e.Row.FindControl("Panel1");
DropDownList t1 = (DropDownList)e.Row.FindControl("DropDownList1");
string p_id = GridView1.ClientID + "_" + p.ClientID;
string ddL_id = GridView1.ClientID + "_" + t1.ClientID;
t1.Attributes.Add("onchange",
string.Format("javascript:showPannel('{0}', '{1}')", p_id, ddL_id ));
thanks for the hint about rendering id
Related
I have a parent and child gridview setup. on the OnRowDataBound event I need the value of a parent gridview cell. this is not the DataKeys of the parent Gridview. I have commented out some of the things I have tried, but not getting quite there. I think the last two lines are close, but I am not sure what the correct syntax should be.
gvCustomers is the parentGV.
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string requestId = gvCustomers.DataKeys[e.Row.RowIndex].Value.ToString();
GridView gvOrders = e.Row.FindControl("gvOrders") as GridView;
gvOrders.DataSource = GetData(string.Format("select top 10 * from Orders where RequestID='{0}'", requestId));
gvOrders.DataBind(); // this is the childGV
string cell_1_Value = gvCustomers.Rows[e.Row.RowIndex].Cells[0].Text;
string cell_2_Value = gvCustomers.Rows[e.Row.RowIndex].Cells[1].Text;
}
}
This is now working using:
foreach (GridViewRow gr in gvCustomers.Rows)
{
string requestStatus = gvCustomers.Rows[gr.RowIndex].Cells[3].Text;
}
But is there anyway to get the same value using the name of the column?
You could try the following:
if (e.Row.RowType == DataControlRowType.DataRow)
{
string requestId = gvCustomers.DataKeys[e.Row.RowIndex].Value.ToString();
GridView gvOrders = e.Row.FindControl("gvOrders") as GridView;
gvOrders.DataSource = GetData(string.Format("select top 10 * from Orders where RequestID='{0}'", requestId));
gvOrders.DataBind(); // this is the childGV
System.Data.DataRowView dr = (System.Data.DataRowView)e.Row.DataItem;
string requestStatus = dr["myColumnHeader"].ToString();
}
}
I have checkboxList which has autopostback true. I have made it in such way in which, on SelectedIndexChanged it gets redirected to same page with querystrting. Querystring value gets generated with selected items. Something like this
www.abcd.com/product?price=2000|3000|5000
So when page gets load the Checkboxlist items gets selected where its value is 2000,3000,5000 etc. But here I have drawback is that when I uncheck any item then agin first it executes pageLoad event code & there it finds value which is uncheck & gets selected again. In short unchecked items gets selected again.
PageLoadevent(Checkbox Items gets selected with querystring values)
string PageUrl = Request.Url.AbsolutePath;
if (PageUrl.Contains("price")) {
string price = Request.QueryString("price");
string[] priceList = price.Split('|');
foreach (string p in priceList) {
if (priceRange.Items.FindByValue(p + "|") != null) {
priceRange.Items.FindByValue(p + "|").Selected = true;
}
}
}
SelectedIndexChanged(URL created with querystring & redirected)
string pageURL = Request.Url.AbsoluteUri;
string strPrice = Request.QueryString("price").ToString;
if (totalcount > 1) {
foreach (ListItem chk in brandsList.Items) {
if (chk.Selected == true) {
selectedBrands += (chk.Value);
}
}
selectedBrands = selectedBrands.Remove(selectedBrands.Length - 1);
Response.Redirect((Request.Url.AbsolutePath + "?") + "&brand=" + selectedBrands + "&price=" + strPrice);
}
if (!IsPostBack)
{
string price = Request.QueryString["price"];
string[] priceList = price.Split('|');
foreach (string p in priceList)
{
if (chkList.Items.FindByText(p) != null)
{
chkList.Items.FindByText(p).Selected = true;
}
}
}
Enclose logic of checkbox selection into IsPostBack condition in page load event. As above. When your page get postback your selection remain as it.
I want to get the values of the selected checkbox in a RadGrid.
I have a radgrid, textbox and a button as follows:
this._RadAjaxPanel.Controls.Add(RadGrid1);
this._RadAjaxPanel.Controls.Add(TextBox1);
this._RadAjaxPanel.Controls.Add(Buton1);
The radgrid id is set to RadGrid1 and
Button1.OnClientClick = "GetSelectedItems("+ this._RadGrid1 +")";
On click of the button a javascript is called where I want to know which rows have been selected.
The javascript function is as follows but it is not correct:
function GetSelectedItems(grid) {
var selectedRows = grid.get_selectedItems();
for (var i = 0; i < selectedRows.length; i++) {
var row = selectedRows[i];
var cell = grid.getCellByColumnUniqueName(row, "CategoryID")
//here cell.innerHTML holds the value of the cell
}
}
Please let me know how can I get the selected rows.
Here is how to get whether or not a checkbox is selected. I am using a GridTemplateColumn with a CheckBox as the ItemTemplate, which Telerik always suggests using over the GridCheckBoxColumn.
The trick is to get the inner HTML in the cell, and parse out the name of the control. The cell value will be something like id=cbxRow where the CheckBox control's ID is cbxRow like in the below example.
JavaScript:
var grid = $find("RadGrid1");
var masterTableView = grid.get_masterTableView();
var selectedRows = masterTableView.get_selectedItems();
for (var i = 0; i < selectedRows.length; i++) {
var cellCB = masterTableView.getCellByColumnUniqueName(row, "CB");
var innerCB = cellCB.innerHTML;
var locId = innerCB.indexOf("id=");
var locIdEnd = innerCB.indexOf("\" ", locId);
var idVal = innerCB.substr(locId + 4, locIdEnd - locId - 4);
var cbx = document.getElementById(idVal);
if (cbx.checked) {
alert("The checkbox is checked!");
}
else {
alert("The checkbox is not checked!");
}
}
ASPX:
<telerik:GridTemplateColumn UniqueName="CB" ...>
<ItemTemplate>
<asp:CheckBox ID="cbxRow" runat="server">
</ItemTemplate>
</telerik:GridTemplateColumn>
I have tried the following, which solved my issue:
this._Button1.Attributes.Add("OnClick", "GetSelectedItems('" + _RadGrid1.ClientID + "');return false;");
I'm trying to generate a control dynamicaly and after change another control. For example i have a button and a label, i want to change label's text using AJAX/JS/etc, without post/get queries. So i'm writing a code like this:
foreach (var pair in FailedMonitorings)
{
var server = (ServerEnum)pair.Key;
var tabPanel = new TabPanel { HeaderText = server.GetDescription() };
var updatePanel = new UpdatePanel();
var upChilds = updatePanel.ContentTemplateContainer.Controls;
var label = new Label { Text = "Hello from UP-" + tabPanel.HeaderText, ID = "lbl" + tabPanel.HeaderText};
upChilds.Add(label);
const string command = #"var ctrl = $.find(""{0}""); alert(ctrl.id); return false;";
var button = new Button {Text = "Button"};
upChilds.Add(button);
tabPanel.Controls.Add(updatePanel);
tbcErrors.Tabs.Add(tabPanel);
button.OnClientClick = string.Format(command, label.ClientID);
}
so generated html here:
for a start i just want to create a messagebox with id of control, but even this simple code doesn't work. Question is: why? And how to solve it?
Added: ctrl is not null
when command is
const string command = #"var ctrl = $.find(""{0}""); alert(((ctrl != null).toString() + ' ') + ctrl.id); return false;";
result is true undefined
I think it should be
const string command = #"var ctrl = $('#{0}')[0]; ctrl.innerText = 'Updated!'; return false;";
Why I say this is in jquery to select an element with an id you would use the same convention as a css selector which means you need to prefix the id with a '#' so $('#elementid') will select for a DOM element like <label id="elementid" /> what I would do is
foreach (var pair in FailedMonitorings)
{
var server = (ServerEnum)pair.Key;
var tabPanel = new TabPanel { HeaderText = server.GetDescription() };
var updatePanel = new UpdatePanel();
var upChilds = updatePanel.ContentTemplateContainer.Controls;
var label = new Label { Text = "Hello from UP-" + tabPanel.HeaderText, ID = "lbl" + tabPanel.HeaderText};
upChilds.Add(label);
const string command = #"updateText('{0}')";
var button = new Button {Text = "Button"};
upChilds.Add(button);
tabPanel.Controls.Add(updatePanel);
tbcErrors.Tabs.Add(tabPanel);
button.OnClientClick = string.Format(command, label.ClientID);
}
then in html
<script type="text/javascript">
function updateText(id){
var ctrl = $('#' + id);
ctrl.text('Updated text');
return false;
}
</script>
I prefer that the button call an updateText function as a posed to rendering all the code inline to the client. It makes the code easier to maintain without having to recompile server side. If you need to work with a dom element then you could use var ctrl = $('#' + id)[0]; this would return the first dom element found for the jquery selection once you have it then you can set the innerText to a value ctrl.innerText = 'Updated!';. I didn't do that in my answer because I just wanted to show how it could be done purely from jquery.
The code
const string command = #"var ctrl = $.find(""{0}""); alert(((ctrl != null).toString() + ' ') + ctrl.id); return false;";
will not work firstly because your jquery selector var ctrl = $.find(""{0}""); will not select the correct control on the client. It would probably render script something like
<button onclick="var ctrl = $.find("Ctrl$Label1");alert(((ctrl != null).toString() + ' ') + ctrl.id); return false;" />
which means that ctrl would not be null but an empty array as there is no dom element like Ctrl$Label1 and since arrays don't have a property of id, it would be undefined. what you are after is a '#' in front of the clientId. The second reason this still will return undefined is that even if you do have a valid selector. Is that the JQuery object returned doesn't have a property of id so you will still see undefined. So finally to get both the id and get the correct selector it would look like
const string command = #"var ctrl = $('#{0}')[0]; alert(ctrl.id); return false;
or if you need to set the text it would look like
const string command =# "var ctrl = $('#{0}')[0]; ctrl.innerText = 'Updated!; return false;
Code:
DataSet ds = _dalEquipmentwiseCheckList.getEquipmentName();
ddEquipmentName.DataSource = ds.Tables[0].DefaultView;
ddEquipmentName.DataTextField = ds.Tables[0].Columns[1].ToString();
ddEquipmentName.DataValueField = ds.Tables[0].Columns[0].ToString();
ddEquipmentName.DataBind();
What I want is: when selecting a row in the GridView, the corresponding equipment name
should get selected in the dropdown list:
var selectRow = MyGridView.SelectedRow;
ddEquipmentName.SelectedValue = selectRow.Cells[2].Text;
****//this is giving me error****
Selected value does not work in this way. Try this:
ddEquipmentName.SelectedIndex = ddEquipmentName.Items.IndexOf(ddEquipmentName.Items.FindByText(selectRow.Cells[2].Text));
You can try :
GridViewRow selectRow = MyGridView.SelectedRow;
ddEquipmentName.selectedItem.Text = selectRow.Cells[2].Text;
void setDataContext()
{
var selectRow = GridEquipmentChechList.MyGridView.SelectedRow;
if (selectRow != null)
{
txtECNumber.Text = selectRow.Cells[1].Text;
**ddEquipmentName.SelectedIndex = ddEquipmentName.Items.IndexOf(ddEquipmentName.Items.FindByText(selectRow.Cells[2].Text));**
**ddDescription.SelectedIndex = ddDescription.Items.IndexOf(ddDescription.Items.FindByText(selectRow.Cells[3].Text));**
}
}