如何更改按钮的功能以及文本 [英] How to change the functionallity of button as well as text of it

查看:85
本文介绍了如何更改按钮的功能以及文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好coder我有一个createuser页面,其中我有一些字段和提交按钮



当我点击我的提交按钮时它会将详细信息保存到数据库中/>


用于createuser页面的aspx.cs代码:



Hi coder i have an createuser page in which i have some field and submit button

When i click on my submit button it will save details into database

aspx.cs code for createuser page:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["WebGallery"].ToString()))
    {
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable dt = new DataTable();

        string strSQL = "SELECT * FROM NewUser WHERE UserName = '" + tbName.Text + "'";

        da.SelectCommand = new SqlCommand(strSQL);
        da.SelectCommand.Connection = con;
        da.Fill(dt);

        if (dt.Rows.Count > 0) // Means first name is already present
        {
            lblmsg.Text = "This user is already added!";
        }
        else if (dt.Rows.Count == 0)
        {
            lblmsg.Visible = false;
            string username = tbName.Text;
            string pwd=tbPassword.Text;
            string Confirmpwd = tbConfirmPassword.Text;
            string Email = tbEmailID.Text;
            string department = ddlDepartment.SelectedValue;
            using (SqlCommand cmd = con.CreateCommand())
            {
                cmd.CommandText = "Insert into NewUser(UserName,Password,ConfirmPassword,EmailID,DepartmentName)values('" + tbName.Text + "','" + tbPassword.Text + "','"+tbConfirmPassword.Text+"','" + tbEmailID.Text + "','" + ddlDepartment.SelectedValue + "')";
                cmd.Parameters.AddWithValue("@FirstName", tbName.Text.Trim());
                cmd.Parameters.AddWithValue("@LastName", tbPassword.Text.Trim());
                cmd.Parameters.AddWithValue("@DomainID", tbConfirmPassword.Text.Trim());
                cmd.Parameters.AddWithValue("@EmailID", tbEmailID.Text.Trim());
                cmd.Parameters.AddWithValue(@"RoleType", ddlDepartment.SelectedValue);
                cmd.ExecuteNonQuery();
            }
            con.Close();
            tbName.Text = "";
            tbPassword.Text = "";
            tbConfirmPassword.Text = "";
            tbEmailID.Text = "";
            tbName.Focus();
        }
    }
}





现在我有一个搜索用户页面我有一个文本框,网格视图和搜索按钮,当我输入任何用户的名称,然后单击搜索按钮,它将显示gridview内的用户详细信息现在我在gridview内有一个编辑链接我想要的是当我点击编辑链接时它将重定向到创建用户页面代替提交按钮我想显示更新按钮,当我进行更改并点击所选用户的更新按钮详细信息时,我将从搜索用户页面的编辑链接中选择更新。我怎么能这样做



aspx搜索用户页面



now i have a search user page in which i have a textbox, gridview and search button when i enter name of any user and click on search button it will show user details inside gridview now i have an edit link inside gridview what i want is when i click on edit link it will redirect to createuser page where in place of submit button i want to show update button and when i make changes and click on update button details for the selected user which i select from edit link of search user page will update. how can i do that

aspx page of search user

<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
            GridLines="None">
            <AlternatingRowStyle BackColor="White" />
           <Columns>
                <asp:HyperLinkField DataNavigateUrlFields="ID" 
                DataNavigateUrlFormatString="~/CreateUser.aspx?ID={0}" 
                HeaderText="Edit" NavigateUrl="~/CreateUser.aspx" Text="Edit"/>
            </Columns>
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>

现在我在gridview里面有一个编辑链接我想要的是当我点击编辑链接时它将重定向到创建用户页面代替提交按钮我想显示更新按钮当我进行更改并单击所选用户的更新按钮详细信息,我将从搜索用户页面的编辑链接中进行选择。我怎么能这样做



提前致谢

Now i have an edit link inside gridview what i want is when i click on edit link it will redirect to createuser page where in place of submit button i want to show update button and when i make changes and click on update button details for the selected user which i select from edit link of search user page will update. how can i do that

Thanks in advance

推荐答案

Amitesh,



你可以试试2件事。第一个是快速而肮脏的方法。



1.设置会话变量Session [CallerPage]。将其设置为添加用户或编辑用户,具体取决于您从哪个模块启动页面。然后在page_Load方法中设置按钮文本。在Button Click事件中,您可以检查Session变量值并根据它们创建If / else块。



2.这个涉及使用查询字符串。每当您执行response.redirect时,都会向CreateUser页面添加一个查询字符串。传递参数以检查请求是来自创建模块还是编辑模块。检查请求的来源后,您可以更改上面第一个选项中提到的按钮的文本和功能。
Amitesh,

You can try 2 things. 1st one is the quick and dirty method.

1. Set a session variable Session["CallerPage"]. Set that to "Add user"or "Edit User" depending upon which module you are launching the page from. Then set the button text in the page_Load method. In the Button Click Event you can check the Session variable value and create your If/else block according to them.

2. This one involves using the Query String. Whenever you are doing response.redirect add a querystring to the CreateUser page. Pass the parameter to check if the request is coming from Create module or Edit module. After you check the origin of request you can change the text and functionality of button as mentioned in the 1st option above.


现在我做了一些更改,它正常工作我写下来希望它为需要它的人工作

创建用户页面::



now i made some change and it work i am writing down it hope it work for some one who need it
on create user page ::

protected void Page_Load(object sender, EventArgs e)
    {
         if (!IsPostBack)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["buttonValue"]))
            {
                    string btnValue = Request.QueryString["buttonValue"];
                    if (btnValue == "Update")
                    {
                        btnSubmit.Text = "Update";
                    }
                    else if (btnValue == "Submit")
                    {
                        btnSubmit.Text = "Submit";
                    }
                    else
                    {
                        Response.Write("error");
                    }
            }
                else
            {
                    this.btnSubmit.Text = "Submit";
            }
        
            string ID = Request.QueryString["ID"];
            cmd = new SqlCommand("Select * from NewUser where ID='" + ID + "'", con);
            con.Open();
            da = new SqlDataAdapter(cmd);
            dt.Clear();
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                tbid.Text = ID;
                tbName.Text = dt.Rows[0][1].ToString();
                tbPassword.Text = dt.Rows[0][2].ToString();
                tbConfirmPassword.Text = dt.Rows[0][3].ToString();
                tbEmailID.Text = dt.Rows[0][4].ToString();
                ddlDepartment.SelectedValue = dt.Rows[0][5].ToString();
            }
        }
        con.Close();
    }





并在搜索用户页面上对gridview超链接进行了一些更改





and on search user page made some change in gridview hyperlink

 <asp:hyperlinkfield datanavigateurlfields="ID" headertext="Edit" xmlns:asp="#unknown">
                   DataNavigateUrlFormatString="~/CreateUser.aspx?ID={0}&buttonValue=Update"
                   NavigateUrl="~/CreateUser.aspx" Text="Edit" />
</asp:hyperlinkfield>



并且可以正常工作


and it work


这篇关于如何更改按钮的功能以及文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆