ImageButton 未获取 GridView 单元格值
ImageButton Not getting the GridView cell value
代码之前是有效的,我所做的只是在 gridview 的 header 模板中添加一个新的文本框。
那么我的代码在做什么:
单击时,我的 gridview 视图上有一个图像按钮,我获取 gridview 当前行的值和单元格 3 的值,并将其设置为等于我的文本框。然后基于该值,我使用模式弹出扩展器使两个细节视图可见,一个细节视图正确呈现但不会运行,除非它从 textbox10.text 获取值作为参数,另一个细节视图甚至不会显示详细信息,因为 select 查询将 textbox10.text 作为参数。
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
if (!IsPostBack)
{
}
else
{
ImageButton imgbtn = (ImageButton)sender;
GridViewRow GridView1 = (GridViewRow)imgbtn.NamingContainer;
string workordernum = GridView1.Cells[3].Text;
TextBox10.Text = workordernum;
//TextBox10.Text = "1";
ModalPopupExtender2.Show();
DetailsView2.Visible = true;
ModalPopupExtender1.Show();
DetailsView1.Visible = true;
}
}
当我将上面的代码置于调试模式时:
调试完成后:
当我设置 textbox10.text = 1 和 运行 应用程序时:
完成值为 1 的应用程序:
.aspx详情视图代码:
<asp:ModalPopupExtender ID="ModalPopupExtender2" runat="server" TargetControlID="pnl2" PopupControlID="pnl2" BackgroundCssClass="modalBackground" DropShadow="False" X="30" Y="300" ValidateRequestMode="Inherit"></asp:ModalPopupExtender>
<asp:HiddenField ID="HiddenField2" runat="server" />
<asp:Panel ID="pnl2" runat="server" Width="881px" Height="175px" CssClass="pnl2BackGround" >
<asp:DetailsView ID="DetailsView2" runat="server" DataSourceID="SqlDataSource3" BackColor="#593B03" Height="175px" Width="881px" Font-Bold="True" ForeColor="White" PostBack = "False" AutoGenerateRows="False" DataKeyNames="WorkOrderNum" >
<Fields>
<asp:BoundField DataField="WorkOrderNum" HeaderText="WorkOrderNum" InsertVisible="False" ReadOnly="True" SortExpression="WorkOrderNum" />
<asp:BoundField DataField="Requestor" HeaderText="Requestor" SortExpression="Requestor" />
<asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" DataFormatString="{0:dd-M-yyyy}" />
<asp:BoundField DataField="Department" HeaderText="Department" SortExpression="Department" />
<asp:BoundField DataField="CompletionDate" HeaderText="CompletionDate" SortExpression="CompletionDate" DataFormatString="{0:dd-M-yyyy}" />
<asp:BoundField DataField="MachineDescription" HeaderText="MachineDescription" SortExpression="MachineDescription" />
<asp:BoundField DataField="MachineLocation" HeaderText="MachineLocation" SortExpression="MachineLocation" />
<asp:BoundField DataField="Type_of_Work_Order" HeaderText="Type_of_Work_Order" SortExpression="Type_of_Work_Order" />
<asp:BoundField DataField="Work_Required" HeaderText="Work_Required" SortExpression="Work_Required" />
</Fields>
</asp:DetailsView>
</asp:Panel>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:WorkOrderConnectionString6 %>" SelectCommand="SELECT [WorkOrderNum], [Requestor], [Date], [Department], [CompletionDate], [MachineDescription], [MachineLocation], [Type of Work Order] AS Type_of_Work_Order, [Work Required] AS Work_Required FROM [Master] WHERE ([WorkOrderNum] = @WorkOrderNum)">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox10" Name="WorkOrderNum" PropertyName="Text" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
我用于从 header 模板搜索 gridview 的代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["Filter"] = "All";
DetailsView1.Visible = false;
TextBox10.Visible = false;
DetailsView2.Visible = false;
DetailsView3.Visible = false;
ReportViewer1.Visible = false;
BindGrid();
}
}
private void BindGrid()
{
DataTable dt = new DataTable();
String WorkOrderConnectionString = System.Configuration.ConfigurationManager
.ConnectionStrings["WorkOrderConnectionString3"].ConnectionString;
SqlConnection con = new SqlConnection(WorkOrderConnectionString);
SqlDataAdapter sda = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand("spx_GetWorkOrderNum");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Filter", ViewState["Filter"].ToString());
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
TextBox TextBox92 = (TextBox)GridView1.HeaderRow.FindControl("WorkOrderNum");
}
protected void TextBox92_TextChanged(object sender, EventArgs e)
{
TextBox TextBox92 = (TextBox)sender;
ViewState["Filter"] = TextBox92.Text;
this.BindGrid();
}
我怀疑 post back 可能是问题所在,代码中的某些地方它做了一个不必要的 post back 所以当我点击图像按钮时它不是获取值
请帮我找出代码中的错误,因为调试器没有帮助我,而且这是之前的工作代码:(
为什么文本框值 = "" 而不是 GRIDVIEW 单元格值??????
谢谢
由于 Gridview 是一个控件,而 TextBox 也是 Gridview 控件中的一个控件,我们需要这样做:
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
ImageButton imgbtn = (ImageButton)sender;
GridViewRow GridView1Row = (GridViewRow)imgbtn.NamingContainer;
Label FLabel = GridView1Row.Cells[2].Controls[0].FindControl("Label1") as Label;
string workordernum = FLabel.Text;// GridView1Row.Cells[3].Text;
TextBox10.Text = workordernum;
ModalPopupExtender2.Show();
DetailsView2.Visible = true;
ModalPopupExtender1.Show();
DetailsView1.Visible = true;
}
代码之前是有效的,我所做的只是在 gridview 的 header 模板中添加一个新的文本框。
那么我的代码在做什么:
单击时,我的 gridview 视图上有一个图像按钮,我获取 gridview 当前行的值和单元格 3 的值,并将其设置为等于我的文本框。然后基于该值,我使用模式弹出扩展器使两个细节视图可见,一个细节视图正确呈现但不会运行,除非它从 textbox10.text 获取值作为参数,另一个细节视图甚至不会显示详细信息,因为 select 查询将 textbox10.text 作为参数。
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
if (!IsPostBack)
{
}
else
{
ImageButton imgbtn = (ImageButton)sender;
GridViewRow GridView1 = (GridViewRow)imgbtn.NamingContainer;
string workordernum = GridView1.Cells[3].Text;
TextBox10.Text = workordernum;
//TextBox10.Text = "1";
ModalPopupExtender2.Show();
DetailsView2.Visible = true;
ModalPopupExtender1.Show();
DetailsView1.Visible = true;
}
}
当我将上面的代码置于调试模式时:
调试完成后:
当我设置 textbox10.text = 1 和 运行 应用程序时:
完成值为 1 的应用程序:
.aspx详情视图代码:
<asp:ModalPopupExtender ID="ModalPopupExtender2" runat="server" TargetControlID="pnl2" PopupControlID="pnl2" BackgroundCssClass="modalBackground" DropShadow="False" X="30" Y="300" ValidateRequestMode="Inherit"></asp:ModalPopupExtender>
<asp:HiddenField ID="HiddenField2" runat="server" />
<asp:Panel ID="pnl2" runat="server" Width="881px" Height="175px" CssClass="pnl2BackGround" >
<asp:DetailsView ID="DetailsView2" runat="server" DataSourceID="SqlDataSource3" BackColor="#593B03" Height="175px" Width="881px" Font-Bold="True" ForeColor="White" PostBack = "False" AutoGenerateRows="False" DataKeyNames="WorkOrderNum" >
<Fields>
<asp:BoundField DataField="WorkOrderNum" HeaderText="WorkOrderNum" InsertVisible="False" ReadOnly="True" SortExpression="WorkOrderNum" />
<asp:BoundField DataField="Requestor" HeaderText="Requestor" SortExpression="Requestor" />
<asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" DataFormatString="{0:dd-M-yyyy}" />
<asp:BoundField DataField="Department" HeaderText="Department" SortExpression="Department" />
<asp:BoundField DataField="CompletionDate" HeaderText="CompletionDate" SortExpression="CompletionDate" DataFormatString="{0:dd-M-yyyy}" />
<asp:BoundField DataField="MachineDescription" HeaderText="MachineDescription" SortExpression="MachineDescription" />
<asp:BoundField DataField="MachineLocation" HeaderText="MachineLocation" SortExpression="MachineLocation" />
<asp:BoundField DataField="Type_of_Work_Order" HeaderText="Type_of_Work_Order" SortExpression="Type_of_Work_Order" />
<asp:BoundField DataField="Work_Required" HeaderText="Work_Required" SortExpression="Work_Required" />
</Fields>
</asp:DetailsView>
</asp:Panel>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:WorkOrderConnectionString6 %>" SelectCommand="SELECT [WorkOrderNum], [Requestor], [Date], [Department], [CompletionDate], [MachineDescription], [MachineLocation], [Type of Work Order] AS Type_of_Work_Order, [Work Required] AS Work_Required FROM [Master] WHERE ([WorkOrderNum] = @WorkOrderNum)">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox10" Name="WorkOrderNum" PropertyName="Text" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
我用于从 header 模板搜索 gridview 的代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["Filter"] = "All";
DetailsView1.Visible = false;
TextBox10.Visible = false;
DetailsView2.Visible = false;
DetailsView3.Visible = false;
ReportViewer1.Visible = false;
BindGrid();
}
}
private void BindGrid()
{
DataTable dt = new DataTable();
String WorkOrderConnectionString = System.Configuration.ConfigurationManager
.ConnectionStrings["WorkOrderConnectionString3"].ConnectionString;
SqlConnection con = new SqlConnection(WorkOrderConnectionString);
SqlDataAdapter sda = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand("spx_GetWorkOrderNum");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Filter", ViewState["Filter"].ToString());
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
TextBox TextBox92 = (TextBox)GridView1.HeaderRow.FindControl("WorkOrderNum");
}
protected void TextBox92_TextChanged(object sender, EventArgs e)
{
TextBox TextBox92 = (TextBox)sender;
ViewState["Filter"] = TextBox92.Text;
this.BindGrid();
}
我怀疑 post back 可能是问题所在,代码中的某些地方它做了一个不必要的 post back 所以当我点击图像按钮时它不是获取值
请帮我找出代码中的错误,因为调试器没有帮助我,而且这是之前的工作代码:(
为什么文本框值 = "" 而不是 GRIDVIEW 单元格值?????? 谢谢
由于 Gridview 是一个控件,而 TextBox 也是 Gridview 控件中的一个控件,我们需要这样做:
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
ImageButton imgbtn = (ImageButton)sender;
GridViewRow GridView1Row = (GridViewRow)imgbtn.NamingContainer;
Label FLabel = GridView1Row.Cells[2].Controls[0].FindControl("Label1") as Label;
string workordernum = FLabel.Text;// GridView1Row.Cells[3].Text;
TextBox10.Text = workordernum;
ModalPopupExtender2.Show();
DetailsView2.Visible = true;
ModalPopupExtender1.Show();
DetailsView1.Visible = true;
}