0

我想使用 Design 更改 gridview 的标题文本。

<asp:BoundField HeaderText="">

我在javascript中创建了一个变量并根据条件初始化变量防御,然后我尝试在此处调用该变量,如下所示:

<asp:BoundField HeaderText="Text Here" DataField="slno" >

在这里,我使用存储在一个变量名称中的“text here”字符串。我想使用那个变量

我的代码:

 <script type="text/javascript" language="javascript">
 /// <summary>
 /// TO ACCESS COOKIE VARIABLE
 /// </summary>
 var flag;
 var ca = document.cookie.split('=');
 for (var i = 0; i < ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0) == ',') c = c.substring(1);
}
if (c != '' && c != null) {
    flag = c;
}
else {
    flag = 1;
}
//*********************************************
if (flag == 1) {
    var name_lbl = "Hai";
}
else if (flag == 2) {
    var name_lbl = "How are you?";
}
</script>
//------------------------------------------------
</asp:Content>
<form id="form1" runat="server">
</table>

<div style="overflow: auto; height: 99%;" id="divTable">
<table><tr><td>
<label for='field ID'>&nbsp;<b><input type="text" id="kilist_lbl" size="20"         style="border: none; height:20px" readonly/></b>&nbsp;&nbsp;</label> <br />
</td></tr></table>

<asp:GridView ID="gvareadetails" runat="server"  
CssClass="mGrid"  AutoGenerateColumns="False">
<Columns>        
  <%-- here is the problem when assigning ID to headerText label --%>
     <asp:BoundField HeaderText='sss' DataField="slno" >  
    <ItemStyle Width="5%" />
    </asp:BoundField>
 </Columns>
 </asp:GridView>
 </div>
 </form>

 <script type="text/javascript" language="javascript">
 document.getElementById('sss').value = name_lbl;
 </script>

 </asp:Content>

任何人都有任何建议如何实现这一目标..

4

2 回答 2

0

而不是 js,只需尝试 C# 代码从网格视图中获取值。

前任:

int name_lbl = gvareadetails.Columns[0][0];

这里Columns[0][0]给你第一行第一列的值。

于 2014-09-17T06:09:07.663 回答
0

从网格视图获取值的 C# 代码:

    string name_lbl;

   protected void Page_Load(object sender, EventArgs e)
   {
    //---------------------------------------------
    /// </summary>
    /// /// <summary>
    /// TO ACCESS COOKIE VARIABLE
    /// </summary>
    string b;

    HttpCookie a = Request.Cookies["LangaugaeObj"];
    if (a != null) b = a.Value; else b = "1";

    if (b == "1")
    {
        name_lbl = "Hai";
    }
    else
    {
        name_lbl = "How are you";
    }
    //---------------------------------------------------
    protected void Button1_Click(object sender, EventArgs e)
    {
      // here I set the variable name to the grid Header
       gvareadetails.Columns[0].HeaderText = name_lbl;

       Report report = new Report();        
       report.areaid = hidStatus.Value.ToString();

       DataSet ds = report.getareawisedetails();

       gvareadetails.DataSource = ds;
       gvareadetails.DataBind();


       ds.Dispose();
       ds = null;
       report = null;
    }

感谢 Shaji SS 的建议。我希望这个答案对那些使用 asp:BoundField 在 asp.net 中动态查找标题文本的人有所帮助。

于 2014-09-17T07:19:29.367 回答