如何查找在rowCommand期间创建并获取其值的控件(文本框)

时间:2014-07-17 16:32:04

标签: c# asp.net gridview

我已将GridView添加到我的webform中。然后我以编程方式将数据绑定到gridview,然后添加一个RowDataBound函数,以便我可以在gridView中选择每个单元格:

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton _singleClickButton = (LinkButton)e.Row.Cells[0].Controls[0];
            string clickInfo = ClientScript.GetPostBackClientHyperlink(_singleClickButton, "");
            // Add events to each editable cell
            for (int columnIndex = 3; columnIndex < e.Row.Cells.Count; columnIndex++)
            {
                // Add the column index as the event argument parameter
                string jsClick = clickInfo.Insert(clickInfo.Length - 2, columnIndex.ToString());
                // Add this javascript to the onclick Attribute of the cell
                e.Row.Cells[columnIndex].Attributes["onclick"] = jsClick;
                // Add a cursor style to the cells
                e.Row.Cells[columnIndex].Attributes["style"] += "cursor:pointer;cursor:hand;";
            }
        }
    }

...那么我想做的是每当选择一个单元格时,将该单元格变为红色并添加一个文本框,这样我就可以输入一个值..如下所示

<Columns>
<asp:ButtonField CommandName="CellClick" Visible="false" ControlStyle-   CssClass="redCell"></asp:ButtonField>
</Columns>

代码隐藏:

  public void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.ToString() == "CellClick")
        {
            //INDEX INFO
            int selectedRowIndex = Convert.ToInt32(e.CommandArgument.ToString());
            int selectedColumnIndex = Convert.ToInt32(Request.Form["__EVENTARGUMENT"].ToString());
            //TRIGGERS EVENT FOR SELECTED CELL
                    GridView1.Rows[selectedRowIndex].Cells[selectedColumnIndex].Attributes["style"] += "background-color:Red;";
                    TextBox scheduleBox = new TextBox();
                    scheduleBox.CssClass = "redCell";
                    scheduleBox.ID = "ActiveCell";
                    scheduleBox.Width = 35;
                    this.GridView1.Rows[selectedRowIndex].Cells[selectedColumnIndex].Controls.Add(scheduleBox);
                    scheduleBox.Focus();
            //LABEL INDEX INFO
            lblCell.Text = (selectedColumnIndex - 2).ToString();
            //LABEL HEADER & ROW TITLES
            lblStartTime.Text = GridView1.Rows[selectedRowIndex].Cells[1].Text;

        } 
    }  GridView1.DataBind();
    }

我现在要做的是,一旦我按下回车键,获取当前驻留在以编程方式创建的texbox中的值,现在只需在消息框或whateevr上显示该值(我真正要做的是更新a数据库,但首先我只需要找出如何获得该值)

<asp:Panel runat="server" DefaultButton="Button1">
<asp:Button ID="Button1" CssClass="ActiveCell" runat="server" Style="display: none"     OnClick="Button1_Click1" /></asp:Panel>

和Im使用的功能是:

protected void Button1_Click1(object sender, EventArgs e)
    {

        var schedule = FindControl("ActiveCell") as TextBox;
        ScriptManager.RegisterStartupScript(this, typeof(Page),
             "alert", "alert('VALUE GOES HERE FROM TEXTBOX');", true);
    }

所以现在我的问题是:如何从ScheduleBox中获取值?

2 个答案:

答案 0 :(得分:0)

如果我理解你的问题,你应该可以使用:

<%=schedule.ClientID %>.value

我确实不是javascript专家,所以如果有帮助请告诉我。

也是
var schedule = FindControl("ActiveCell") as TextBox;

正确返回文本框?

编辑:如果这不起作用,请尝试

<%=ActiveCell.ClientID %>.value

答案 1 :(得分:0)

您是否尝试过查看所选行的单元格的控件? 要解决GridView中缺少selectedColumnIndex的问题,我不得不更改“GridView1_RowCommand”事件来替换一行(设置ID)并添加一行:

            scheduleBox.ID = "ActiveCell_" + selectedRowIndex.ToString() + "_" + selectedColumnIndex.ToString();
            scheduleBox.TextChanged += scheduleBox_TextChanged;

它看起来像这样:

int selectedColumnIndex = 0;
int selectedRowIndex = 0;
string lastUserInputText = string.Empty;

    public void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.ToString() == "CellClick")
        {
            //INDEX INFO
            selectedRowIndex = Convert.ToInt32(e.CommandArgument.ToString());
            selectedColumnIndex = Convert.ToInt32(Request.Form["__EVENTARGUMENT"].ToString());
            //TRIGGERS EVENT FOR SELECTED CELL
            GridView1.Rows[selectedRowIndex].Cells[selectedColumnIndex].Attributes["style"] += "background-color:Red;";
            TextBox scheduleBox = new TextBox();
            scheduleBox.CssClass = "redCell";

            //This formats the ID so its unique, and now the TextBox contains the row and colummn indexes:
            scheduleBox.ID = "ActiveCell_" + selectedRowIndex.ToString() + "_" + selectedColumnIndex.ToString();
            scheduleBox.TextChanged += scheduleBox_TextChanged;

            scheduleBox.Width = 35;

            this.GridView1.Rows[selectedRowIndex].Cells[selectedColumnIndex].Controls.Add(scheduleBox);
            scheduleBox.Focus();
            //LABEL INDEX INFO
            lblCell.Text = (selectedColumnIndex - 2).ToString();
            ////LABEL HEADER & ROW TITLES
            lblStartTime.Text = GridView1.Rows[selectedRowIndex].Cells[1].Text;

        }
        GridView1.DataBind();
    }

//The following event gets the current index of the Row and the column where the user is changing the text
    void scheduleBox_TextChanged(object sender, EventArgs e)
    {
        TextBox txtSelected = (TextBox)sender;
        string[] selectedValues = txtSelected.ID.Split(new char[] { '_' });
        selectedRowIndex = int.Parse(selectedValues[1]);
        selectedColumnIndex = int.Parse(selectedValues[2]);
        //you could also use it to get the text directly while the user is editing it:
        lastUserInputText = txtSelected.Text;
    }

//This gets the text for the selected row and column. But if you only have 1 column with a TextBox it would be easier to just use the column index constant instead of doing it dynamically. However, remember you already have this value in the "lastUserInputText" variable. If you use that the following code may not be necessary:
string GetTextFromSelectedRowTextBox()
{
    string textBoxValue = string.Empty;
    foreach (Control curControl in this.GridView1.Rows[selectedRowIndex].Cells[selectedColumnIndex].Controls)
    {
        if (curControl is TextBox)
        {
            TextBox txtScheduleBox = (TextBox)curControl;
            textBoxValue = txtScheduleBox.Text;
            break;
        }
    }
    return textBoxValue;
}