本文介绍了当我单击单元格时如何查找gridview单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
我正在开发电影院座位预订网站,为此,我已使用ImageButton模板将图像作为座位"放置在asp:gridview(所有单元格)中,以选择当时在ImageButton(cell)上单击的座位,我需要单击的那个单元格值gridview中的ImageButton.
我得到的是RowIndex但不是ColIndex.
Hi to all
I am developing movie theater seats booking website, for that i have placed images as Seats in asp:gridview(all cells) using ImageButton Template, for selection of seat click on ImageButton(cell) at that time i need that cell value where i clicked ImageButton in gridview.
I am getting RowIndex but not ColIndex.
<asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="False"
Width="755px" AllowPaging="True"
PageSize="26" ShowHeader="true" CellSpacing="1"
onrowupdating="Gridview1_RowUpdating"
onrowcommand="Gridview1_RowCommand">
<rowstyle borderstyle="None" />
<columns>
<asp:TemplateField >
<itemtemplate>
<asp:HyperLink ID="HyperLink1" Text='<%# Eval("SeatRow") %>' runat="server">
</itemtemplate>
<itemstyle borderstyle="None" />
<asp:TemplateField HeaderText ="1" >
<itemtemplate>
<asp:ImageButton ID="ImageButton1" CommandArgument='<%# Eval("C1") %>' CommandName="ib1"
Visible='<%# MyVisible(Eval("C1").ToString()) %>' runat="server" ImageUrl='<%# MyImg(Eval("C1").ToString()) %>' Text='<%# MyValue(Eval("C1").ToString()) %>' OnClick ="ImageButton1_Click"/>
</itemtemplate>
<itemstyle borderstyle="None" />
<asp:TemplateField HeaderText ="2">
<itemtemplate>
<asp:ImageButton ID="ImageButton2" runat="server" CommandArgument='<%# Eval("C2") %>'
CommandName="ib2" Visible='<%# MyVisible(Eval("C2").ToString()) %>' ImageUrl='<%# MyImg(Eval("C2").ToString()) %>'
OnClick="button_click" RowIndex='<%# Container.DisplayIndex %>'/>
</itemtemplate>
<itemstyle borderstyle="None" />
<asp:TemplateField HeaderText ="3">
<itemtemplate>
<asp:ImageButton ID="ImageButton3" runat="server" CommandArgument='<%# Eval("C3") %>'
CommandName="ib3" Visible='<%# MyVisible(Eval("C3").ToString()) %>' ImageUrl='<%# MyImg(Eval("C3").ToString()) %>'/>
</itemtemplate>
<itemstyle borderstyle="None" />
</columns>
<footerstyle borderstyle="None" />
<SelectedRowStyle BorderStyle="None" />
<editrowstyle borderstyle="None" />
<alternatingrowstyle borderstyle="None" />
Defaust.aspx.cs
Defaust.aspx.cs
public bool MyVisible(string txt)
{
txtNum = txt;
if (txt == "")
{
return false;
//Gridview1
}
else { return true; }
}
string ImgPath = "";
public string MyImg(string txt)
{
try
{
if (txtNum == "1")
ImgPath = "~/Images/wchair.JPG";
else
ImgPath = "~/Images/wchair.JPG";
return ImgPath;
}
catch (Exception ex)
{
throw;
}
}
protected void gvScreenLayout_RowCommand(object sender, GridViewCommandEventArgs e)
{
int id = (int)gvScreenLayout.DataKeys[Convert.ToInt32(e.CommandArgument)].Value;
}
推荐答案
private void GridView1_Cell_Click(object sender, GridViewCellEventArgs e)
{
int cellindex=e.CellIndex;
int rowindex =e.rowindex;
var value = GridView1.Rows[rowindex].Cells[cellindex].value;
}
<asp:gridview pagerstyle-cssclass="dataGridPager" onpageindexchanging="pageIndexChange" onselectedindexchanged="selectedRowChanged" runat="server" onrowdatabound="machineGridView" xmlns:asp="#unknown">
PageSize="25" UseAccessibleHeader="true" AllowPaging="true" SelectedRowStyle-BackColor="Blue"
ID="dataGrid" CssClass="dataGrid" >
<alternatingrowstyle backcolor="WhiteSmoke" horizontalalign="Center" />
<headerstyle font-bold="true" horizontalalign="Center" backcolor="#2C4E5B" forecolor="White" />
<pagersettings visible="true" mode="Numeric" />
<pagerstyle horizontalalign="Center" font-underline="true" />
<selectedrowstyle font-bold="true" />
</asp:gridview>
例如,您具有上面的dataGrid
在您的.cs文件中尝试一下
For example you have dataGrid like above
Try this in your .cs file
protected void machineGridView(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onmouseover"] = "this.style.color='red';";
e.Row.Attributes["onmouseout"] = "this.style.color='black';";
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.dataGrid, "Select
这篇关于当我单击单元格时如何查找gridview单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!