本文介绍了如何在按钮单击上完成两项工作并在条件下加载不同的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 当我点击搜索申请人按钮按申请人身份填写网格但我选择申请人姓名并点击按钮不显示网格或任何数据。 if (!IsPostBack) { ddlHeader.Items.Insert( 0 , ---选择---); ddlHeader.Items.Insert( 1 , ApplicantID); ddlHeader.Items.Insert( 2 , ApplicantName); GridView2.DataSource = applic.ShowApplicant(); GridView2.DataBind(); } < pre lang = C# > < b>方法 1 < / b > public DataSet GetApplicantDataByID( string ApplicantID) { ds = new DataSet(); ds = SqlHelper.ExecuteDataset(con,CommandType.Text,& quot; 选择 * 来自申请人其中 ApplicantId =&#39;& quot; + ApplicantID +& quot;&#39;& quot;); return ds; } < b>方法 2 < / b > public DataSet GetApplicantDataByName( string 名称) { ds = new 数据集(); ds = SqlHelper.ExecuteDataset(con,CommandType.Text,& quot; 选择 * 来自申请人其中 ApplicantName =&#39;& quot; + ApplicantName +& quot;&#39;& quot;); return ds; } & lt; pre lang =& quot; C#& quot;& gt; GetApplicantDataByID(txtApp.Text) & amp; lt; pre lang =& amp; quot; C#& amp; quot;& amp; gt; protected void btnSearchApplicant_Click( object sender,EventArgs e) { if (ddlHeader.SelectedIndex == 1 ) { GridView2.DataSource = applic.GetApplicantDataByID( txtApp.Text); GridView2.DataBind(); } else if (ddlHeader.SelectedIndex == 2 ) { GridView2.DataSource = applic.GetApplicantDataByName(txtApp.Text); GridView2.DataBind(); } else { btnSearchApplicant.Visible = 假; txtApp.Visible = false ; } }& amp; lt; / pre& amp; gt;& lt; / pre& gt; < / pre > 解决方案 我猜你假设如果它是一个回发按下你的按钮之一。虽然这样做有点工作,但当你有多个按钮时,无法知道哪个按钮导致了回发。因此,您需要为按钮实现点击事件,并将相关代码放入相关的点击事件中。 When I click on Search Applicant Button By Applicant Id filled on Grid but when i choose Applicant name and click on button Not show grid or any data. if (!IsPostBack) { ddlHeader.Items.Insert(0, "---Select---"); ddlHeader.Items.Insert(1, "ApplicantID"); ddlHeader.Items.Insert(2, "ApplicantName"); GridView2.DataSource = applic.ShowApplicant(); GridView2.DataBind(); }<pre lang="C#"><b>Method 1</b>public DataSet GetApplicantDataByID(string ApplicantID) { ds = new DataSet(); ds = SqlHelper.ExecuteDataset(con, CommandType.Text, &quot;select * from Applicant where ApplicantId=&#39;&quot;+ApplicantID +&quot;&#39;&quot;); return ds; }<b>Method 2</b> public DataSet GetApplicantDataByName(string Name) { ds = new DataSet(); ds = SqlHelper.ExecuteDataset(con, CommandType.Text, &quot;select * from Applicant where ApplicantName=&#39;&quot; + ApplicantName + &quot;&#39;&quot;); return ds; }&lt;pre lang=&quot;C#&quot;&gt;GetApplicantDataByID(txtApp.Text)&amp;lt;pre lang=&amp;quot;C#&amp;quot;&amp;gt;protected void btnSearchApplicant_Click(object sender, EventArgs e) { if (ddlHeader.SelectedIndex == 1) { GridView2.DataSource = applic.GetApplicantDataByID(txtApp.Text); GridView2.DataBind(); } else if (ddlHeader.SelectedIndex == 2) { GridView2.DataSource = applic.GetApplicantDataByName(txtApp.Text); GridView2.DataBind(); } else { btnSearchApplicant.Visible=false; txtApp.Visible=false; } }&amp;lt;/pre&amp;gt;&lt;/pre&gt;</pre> 解决方案 I'm guessing you are assuming that if it is a postback one of your buttons has been pressed. While this does kind of work, when you have multiple buttons there is no way of knowing which button caused the postback. So you need to implement click events for the buttons and put the relevant code in the relevant click event. 这篇关于如何在按钮单击上完成两项工作并在条件下加载不同的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-02 01:50