Dear sir
I will explain more to you.
After I did "this.cleintID" and it worked, but I found that it is not perfect.
I want tell you that this usercontrol 'X' contain an update panel + timer tick + gridview which is refreshed every 5 sec.
The idea is that everything is working very good even in multiple user controls from the same ID.
The problem was that, the javascript function, but I did make the "this.cleintID".
How dose it works, every 5 sec I hit the server and I get the new data as a dataset and I bind it on the grid view, and I add it to a view state because the next 5 sec I get the new data and then bind it the girdview1 and the bind the viewstate to the gridview2 and then from javascript I can make some comparison, which will make cells of gridview1 colored to green and Red, after 2 sec I call other javascript function that reset all the colors.
If I run only 1 usercontrol then it is working very well,
If I run 2 usercontrols from the same name then it is not working good.
Please advise what to do.
please find
1 - all the HTML + JavaScript
2 - the method i call every 5 Sec.
This is the client side javascript:
<script language="javascript" type="text/javascript">
// all the scripts have to wait for the whole page to be loaded (including external files like js libraries)
if (window.addEventListener)
window.addEventListener("load", PageOnLoad, false); // Mozilla, Netscape, Firefox
else
window.attachEvent("onload", PageOnLoad); // IE
// this is called only the first time and NOT on AJAX updates
function PageOnLoad()
{
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(PageLoadedHandler<%=this.ClientID %>);
}
// this is called the first time AND on full postbacks AND on AJAX updates
function PageLoadedHandler<%=this.ClientID %>(sender, args)
{
BlinkValuationViewCellsVV<%=this.ClientID %>();
}
function BlinkValuationViewCellsVV<%=this.ClientID %>()
{
if (document.getElementById('<%=inputGridRowCount.ClientID%>').value == "0")
{
return;
}
if (document.getElementById('<%=inputGridRowCount2.ClientID%>').value == "0")
{
return;
}
var GV1 = document.getElementById('<%=GridView1.ClientID%>');
var GV2 = document.getElementById('<%=GridView2.ClientID%>');
//
//alert(document.getElementById('<%=inputGridRowCount.ClientID%>').value);
//alert(document.getElementById('<%=inputGridRowCount2.ClientID%>').value);
//
//return;
if (GV1.rows[1].cells[1].innerHTML == " ")
{
return;
}
//
// alert(<%=this.ClientID %>)
for (i = 1 ; i <= document.getElementById('<%=inputGridRowCount.ClientID%>').value ; i++)
{
var nGvRouterCode1 = GV1.rows[i].cells[0].innerHTML;
var nGvRouterCode2 = GV2.rows[i].cells[0].innerHTML;
//alert(nGvRouterCode1 + " " + nGvRouterCode2);
if (nGvRouterCode1 == nGvRouterCode2)
{
var ihfMarketPrice = document.getElementById('<%=inputMarketPrice.ClientID%>').value;
var ihfValuation = document.getElementById('<%=inputValuation.ClientID%>').value;
var ihfGainLoss = document.getElementById('<%=inputGainLoss.ClientID%>').value;
var ihfGainLossPer = document.getElementById('<%=inputGainLossPer.ClientID%>').value;
//One
if (ihfMarketPrice != "-1")
{
var nGvMarketPrice1 = GV1.rows[i].cells[ihfMarketPrice].innerHTML;
var nGvMarketPrice2 = GV2.rows[i].cells[ihfMarketPrice].innerHTML;
if (nGvMarketPrice1 > nGvMarketPrice2)
{
alert(nGvMarketPrice1 +" "+ nGvMarketPrice2);
GV1.rows[i].cells[ihfMarketPrice].style.color = "black";
GV1.rows[i].cells[ihfMarketPrice].style.backgroundColor = "green";
}
if (nGvMarketPrice1 < nGvMarketPrice2)
{
GV1.rows[i].cells[ihfMarketPrice].style.color = "black";
GV1.rows[i].cells[ihfMarketPrice].style.backgroundColor = "red";
}
}
//Second
if (ihfValuation != "-1")
{
var nGvValuation1 = GV1.rows[i].cells[ihfValuation].innerHTML;
var nGvValuation2 = GV2.rows[i].cells[ihfValuation].innerHTML;
if (nGvValuation1 > nGvValuation2)
{
GV1.rows[i].cells[ihfValuation].style.color = "black";
GV1.rows[i].cells[ihfValuation].style.backgroundColor = "green";
}
if (nGvValuation1 > nGvValuation2)
{
GV1.rows[i].cells[ihfValuation].style.color = "black";
GV1.rows[i].cells[ihfValuation].style.backgroundColor = "red";
}
}
//Third
if (ihfGainLoss != "-1")
{
var nGvGainLoss1 = GV1.rows[i].cells[ihfGainLoss].innerHTML;
var nGvGainLoss2 = GV2.rows[i].cells[ihfGainLoss].innerHTML;
if (nGvGainLoss1 > nGvGainLoss2)
{
GV1.rows[i].cells[ihfGainLoss].style.color = "black";
GV1.rows[i].cells[ihfGainLoss].style.backgroundColor = "green";
}
if (nGvGainLoss1 < nGvGainLoss2)
{
GV1.rows[i].cells[ihfGainLoss].style.color = "black";
GV1.rows[i].cells[ihfGainLoss].style.backgroundColor = "red";
}
}
//Fourth
if (ihfGainLossPer != "-1")
{
var nGvGainLossPer1 = GV1.rows[i].cells[ihfGainLossPer].innerHTML;
var nGvGainLossPer2 = GV2.rows[i].cells[ihfGainLossPer].innerHTML;
if (nGvGainLossPer1 > nGvGainLossPer2)
{
GV1.rows[i].cells[ihfGainLossPer].style.color = "black";
GV1.rows[i].cells[ihfGainLossPer].style.backgroundColor = "green";
}
if (nGvGainLossPer1 < nGvGainLossPer2)
{
GV1.rows[i].cells[ihfGainLossPer].style.color = "black";
GV1.rows[i].cells[ihfGainLossPer].style.backgroundColor = "red";
}
}
}
}
setTimeout("AfterValuationViewSleepVV<%=this.ClientID %>();", 2000);
}
function AfterValuationViewSleepVV<%=this.ClientID %>()
{
var GV1 = document.getElementById('<%=GridView1.ClientID%>');
var ihfMarketPrice = document.getElementById('<%=inputMarketPrice.ClientID%>').value;
var ihfValuation = document.getElementById('<%=inputValuation.ClientID%>').value;
var ihfGainLoss = document.getElementById('<%=inputGainLoss.ClientID%>').value;
var ihfGainLossPer = document.getElementById('<%=inputGainLossPer.ClientID%>').value;
for (i = 1 ; i <= document.getElementById('<%=inputGridRowCount.ClientID%>').value ; i++)
{
GV1.rows[i].cells[ihfMarketPrice].style.backgroundColor = "";
GV1.rows[i].cells[ihfValuation].style.backgroundColor = "";
GV1.rows[i].cells[ihfGainLoss].style.backgroundColor = "";
GV1.rows[i].cells[ihfGainLossPer].style.backgroundColor = "";
//
var nGvMarketPrice1 = GV1.rows[i].cells[ihfMarketPrice].innerHTML;
var nGvValuation1 = GV1.rows[i].cells[ihfValuation].innerHTML;
var nGvGainLoss1 = GV1.rows[i].cells[ihfGainLoss].innerHTML;
var nGvGainLossPer1 = GV1.rows[i].cells[ihfGainLossPer].innerHTML;
//One
if (nGvMarketPrice1 > 0)
{
GV1.rows[i].cells[ihfMarketPrice].style.color = "green";
}
else if (nGvMarketPrice1 < 0)
{
GV1.rows[i].cells[ihfMarketPrice].style.color = "red";
}
//Two
if (nGvValuation1 > 0)
{
GV1.rows[i].cells[ihfValuation].style.color = "green";
}
else if (nGvValuation1 < 0)
{
GV1.rows[i].cells[ihfValuation].style.color = "red";
}
//Third
if (nGvGainLoss1 > 0)
{
GV1.rows[i].cells[ihfGainLoss].style.color = "green";
}
else if (nGvGainLoss1 < 0)
{
GV1.rows[i].cells[ihfGainLoss].style.color = "red";
}
//Fourth
if (nGvGainLossPer1 > 0)
{
GV1.rows[i].cells[ihfGainLossPer].style.color = "green";
}
else if (nGvGainLossPer1 < 0)
{
GV1.rows[i].cells[ihfGainLossPer].style.color = "red";
}
}
}
</script>
<table id="tMaster" runat="server">
<tr>
<td style="padding-left: 10px; font-weight: bold; font-size: 11pt; vertical-align: top; color: #cc0000; padding-top: 12px; height: 35px;" id="tdTitle" runat="server">
</td>
</tr>
<tr>
<td>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<table>
<tr>
<td style="width: 100px">
<asp:Timer ID="Timer1" runat="server" Interval="5000" OnTick="Timer1_Tick" Enabled="False">
</asp:Timer>
</td>
</tr>
<tr>
<td style="width: 100px">
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" OnDataBound="GridView1_DataBound"
OnRowDataBound="GridView1_RowDataBound" OnRowDeleting="GridView1_RowDeleting"
OnSorting="GridView1_Sorting" ShowFooter="True" AutoGenerateColumns="False">
</asp:GridView>
</td>
</tr>
<tr>
<td style="width: 100px">
<input id="inputMarketPrice" type="hidden" runat="server" value="-1" /><input id="inputRowIndex" type="hidden" runat="server" /><input id="inputSelectedCode" type="hidden" runat="server" /><input id="inputGridRowCount2" type="hidden" runat="server" value="0" /><input id="inputGridRowCount" type="hidden" runat="server" value="0"/><input id="inputValuation" type="hidden" runat="server" value="-1"/><input id="inputGainLoss" type="hidden" runat="server" value="-1"/><input id="inputGainLossPer" type="hidden" runat="server" value="-1"/></td>
</tr>
<tr>
<td style="display:none">
<asp:GridView ID="GridView2" runat="server">
</asp:GridView>
</td>
</tr>
</table>
<br />
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
</table>
This is the method that I call every 5 Sec from C#:
public void LoadData(string strSortBy)
{
if (ViewState["_sPortfolioID" + this.ClientID.ToString()] == null)
{
return;
}
DataSet _dsMyPortfolio;
_dsMyPortfolio = tradeService.GetPortfolioValuationView(Session["sUserName"].ToString(), Session["sPassword"].ToString(), Convert.ToUInt32(ViewState["_sPortfolioID" + this.ClientID.ToString()].ToString()));
if (_dsMyPortfolio == null || _dsMyPortfolio.Tables[0].Rows.Count == 0)
{
this.DataSetNoRows();
}
else
{
if (this.GridView1.Columns.Count == 0)
{
for (int i = 0; i < _dsMyPortfolio.Tables[0].Columns.Count; i++)
{
if (_dsMyPortfolio.Tables[0].Columns[i].ColumnName == "VVReutersCode")
{
BoundField bfNewField = new BoundField();
bfNewField.HeaderText = "Reuters Code";
bfNewField.DataField = "VVReutersCode";
bfNewField.SortExpression = "VVReutersCode";
bfNewField.DataFormatString = "";
bfNewField.HtmlEncode = false;
GridView1.Columns.Add(bfNewField);
}
else if (_dsMyPortfolio.Tables[0].Columns[i].ColumnName == "VVHolding")
{
BoundField bfNewField = new BoundField();
bfNewField.HeaderText = "Holding";
bfNewField.DataField = "VVHolding";
bfNewField.SortExpression = "VVHolding";
bfNewField.DataFormatString = "";
bfNewField.HtmlEncode = false;
GridView1.Columns.Add(bfNewField);
}
else if (_dsMyPortfolio.Tables[0].Columns[i].ColumnName == "VVAverageCost")
{
BoundField bfNewField = new BoundField();
bfNewField.HeaderText = "Average Cost";
bfNewField.DataField = "VVAverageCost";
bfNewField.SortExpression = "VVAverageCost";
bfNewField.DataFormatString = "{0:###,###,##0.00}";
bfNewField.HtmlEncode = false;
GridView1.Columns.Add(bfNewField);
}
else if (_dsMyPortfolio.Tables[0].Columns[i].ColumnName == "VVMarketPrice")
{
BoundField bfNewField = new BoundField();
bfNewField.HeaderText = "Market Price";
bfNewField.DataField = "VVMarketPrice";
bfNewField.SortExpression = "VVMarketPrice";
bfNewField.DataFormatString = "{0:###,###,##0.00}";
bfNewField.HtmlEncode = false;
GridView1.Columns.Add(bfNewField);
this.inputMarketPrice.Value = Convert.ToString(i);
}
else if (_dsMyPortfolio.Tables[0].Columns[i].ColumnName == "VVCostBasis")
{
BoundField bfNewField = new BoundField();
bfNewField.HeaderText = "Cost Basis";
bfNewField.DataField = "VVCostBasis";
bfNewField.SortExpression = "VVCostBasis";
bfNewField.DataFormatString = "{0:###,###,##0.00}";
bfNewField.HtmlEncode = false;
GridView1.Columns.Add(bfNewField);
}
else if (_dsMyPortfolio.Tables[0].Columns[i].ColumnName == "VVValuation")
{
BoundField bfNewField = new BoundField();
bfNewField.HeaderText = "Valuation";
bfNewField.DataField = "VVValuation";
bfNewField.SortExpression = "VVValuation";
bfNewField.DataFormatString = "{0:###,###,##0.00}";
bfNewField.HtmlEncode = false;
GridView1.Columns.Add(bfNewField);
this.inputValuation.Value = Convert.ToString(i);
}
else if (_dsMyPortfolio.Tables[0].Columns[i].ColumnName == "VVGainLoss")
{
BoundField bfNewField = new BoundField();
bfNewField.HeaderText = "Gain Loss";
bfNewField.DataField = "VVGainLoss";
bfNewField.SortExpression = "VVGainLoss";
bfNewField.DataFormatString = "{0:###,###,##0.00}";
bfNewField.HtmlEncode = false;
GridView1.Columns.Add(bfNewField);
this.inputGainLoss.Value = Convert.ToString(i);
}
else if (_dsMyPortfolio.Tables[0].Columns[i].ColumnName == "VVGainLossPercentage")
{
BoundField bfNewField = new BoundField();
bfNewField.HeaderText = "Gain Loss %";
bfNewField.DataField = "VVGainLossPercentage";
bfNewField.SortExpression = "VVGainLossPercentage";
bfNewField.DataFormatString = "{0:c}";
bfNewField.HtmlEncode = false;
GridView1.Columns.Add(bfNewField);
this.inputGainLossPer.Value = Convert.ToString(i);
}
}
}
if (ViewState["vsValuationView" + this.ClientID.ToString()] != null)
{
dv = ((DataSet)ViewState["vsValuationView" + this.ClientID.ToString()]).Tables[0].DefaultView;
this.inputGridRowCount2.Value = ((DataSet)ViewState["vsValuationView" + this.ClientID.ToString()]).Tables[0].Rows.Count.ToString();
GridView2.DataSource = dv;
dv.Sort = strSortBy;
GridView2.DataBind();
}
ViewState.Add("vsValuationView" + this.ClientID.ToString(), _dsMyPortfolio);
this.inputGridRowCount.Value = _dsMyPortfolio.Tables[0].Rows.Count.ToString();
dv = _dsMyPortfolio.Tables[0].DefaultView;
GridView1.DataSource = dv;
dv.Sort = strSortBy;
GridView1.DataBind();
if (this.Timer1.Enabled == false)
{
this.Timer1.Enabled = true;
this.Timer1.Interval = 5000;
}
}
}
thank you
please advise what to do.